Skip to content

Commit

Permalink
Add rds security ip
Browse files Browse the repository at this point in the history
  • Loading branch information
PerhapsDy committed Nov 13, 2023
1 parent bf0a5ec commit f9639c6
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 1.19.20 (Unreleased)
## 1.19.21 (Unreleased)
## 1.19.20 (November 13, 2023)
NOTES:
- ADD RDS Security ip.


## 1.19.19 (October 17, 2023)
NOTES:
- update resource/baiducloud_cds storage_type description : add `enhanced_ssd_pl1`.
Expand Down
1 change: 1 addition & 0 deletions baiducloud/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
BaiduCloudTestResourceTypeNameRdsAccount = BaiduCloudTestResourceTypeName + "-" + "rds-account"
BaiduCloudTestResourceTypeNameRdsInstance = BaiduCloudTestResourceTypeName + "-" + "rds-instance"
BaiduCloudTestResourceTypeNameRdsReadonlyInstance = BaiduCloudTestResourceTypeName + "-" + "rds-readonly-instance"
BaiduCloudTestResourceTypeNameRdsSecurityIp = BaiduCloudTestResourceTypeName + "-" + "rds-security-ip"
BaiduCloudTestResourceTypeNameRouteRule = BaiduCloudTestResourceTypeName + "-" + "route-rule"
BaiduCloudTestResourceTypeNameScs = BaiduCloudTestResourceTypeName + "-" + "scs"
BaiduCloudTestResourceTypeNameSecurityGroup = BaiduCloudTestResourceTypeName + "-" + "security-group"
Expand Down
98 changes: 98 additions & 0 deletions baiducloud/data_source_baiducloud_rds_security_ips.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Use this data source to query RDS security ips.
Example Usage
```hcl
data "baiducloud_rds_security_ips" "default" {
instance_id = "rds-LCP5Tn03"
}
output "security_ips" {
value = "${data.baiducloud_rdss.default.security_ips}"
}
```
*/
package baiducloud

import (
"github.com/baidubce/bce-sdk-go/services/rds"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)

func dataSourceBaiduCloudRdsSecurityIps() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudRdssSecurityIpsRead,

Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Description: "ID of the instance",
Required: true,
ForceNew: true,
},
"security_ips": {
Type: schema.TypeList,
Description: "security_ips",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Description: "securityIp",
Computed: true,
},
},
},
},
},
}
}

func dataSourceBaiduCloudRdssSecurityIpsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)

instanceID := d.Get("instance_id").(string)
action := "Query RDS SecurityIp instanceID is " + instanceID

raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return rdsClient.GetSecurityIps(instanceID)
})

addDebug(action, raw)

if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_rds_security_ips", action, BCESDKGoERROR)
}

securityIpsResult, _ := raw.(*rds.GetSecurityIpsResult)
securityIps := make([]map[string]interface{}, 0)
for _, ip := range securityIpsResult.SecurityIps {
ipMap := make(map[string]interface{})
ipMap["ip"] = ip
securityIps = append(securityIps, ipMap)
}
addDebug(action, securityIps)

FilterDataSourceResult(d, &securityIps)

if err := d.Set("security_ips", securityIps); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_rds_security_ips", action, BCESDKGoERROR)
}

d.SetId(resource.UniqueId())

if v, ok := d.GetOk("output_file"); ok && v.(string) != "" {
if err := writeToFile(v.(string), securityIps); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_rds_security_ips", action, BCESDKGoERROR)
}
}
return nil
}
40 changes: 40 additions & 0 deletions baiducloud/data_source_baiducloud_rds_security_ips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package baiducloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

const (
testAccRdsSecurityIpDataSourceName = "data.baiducloud_rds_security_ips.default"
testAccRdsSecurityIpDataSourceAttrKeyPrefix = "security_ips.0."
)

//lintignore:AT003
func TestAccBaiduCloudRdsSecurityIpDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccRdsSecurityIpDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckBaiduCloudDataSourceId(testAccRdsSecurityIpDataSourceName),
resource.TestCheckResourceAttrSet(testAccRdsSecurityIpDataSourceName, testAccRdsSecurityIpDataSourceAttrKeyPrefix+"ip"),
),
},
},
})
}

func testAccRdsSecurityIpDataSourceConfig() string {
return fmt.Sprintf(`
data "baiducloud_rds_security_ips" "default" {
instance_id = "rds-BIFDrIl9"
}
`)
}
7 changes: 4 additions & 3 deletions baiducloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,13 @@ package baiducloud
import (
"bytes"
"fmt"
"os"
"strings"

"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/bcc"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/bec"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/cdn"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/iam"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/snic"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -213,6 +212,7 @@ func Provider() terraform.ResourceProvider {
"baiducloud_ccev2_instance_group_instances": dataSourceBaiduCloudCCEv2InstanceGroupInstances(),
"baiducloud_cce_kubeconfig": dataSourceBaiduCloudCceKubeConfig(),
"baiducloud_rdss": dataSourceBaiduCloudRdss(),
"baiducloud_rds_security_ips": dataSourceBaiduCloudRdsSecurityIps(),
"baiducloud_dtss": dataSourceBaiduCloudDtss(),
"baiducloud_cdn_domains": cdn.DataSourceDomains(),
"baiducloud_cdn_domain_certificate": cdn.DataSourceDomainCertificate(),
Expand Down Expand Up @@ -278,6 +278,7 @@ func Provider() terraform.ResourceProvider {
"baiducloud_rds_instance": resourceBaiduCloudRdsInstance(),
"baiducloud_rds_readonly_instance": resourceBaiduCloudRdsReadOnlyInstance(),
"baiducloud_rds_account": resourceBaiduCloudRdsAccount(),
"baiducloud_rds_security_ip": resourceBaiduCloudRdsSecurityIp(),
"baiducloud_dts": resourceBaiduCloudDts(),
"baiducloud_iam_user": resourceBaiduCloudIamUser(),
"baiducloud_iam_group": resourceBaiduCloudIamGroup(),
Expand Down
Loading

0 comments on commit f9639c6

Please sign in to comment.