Skip to content

Commit

Permalink
Add SCS security ip
Browse files Browse the repository at this point in the history
  • Loading branch information
PerhapsDy committed Nov 27, 2023
1 parent b5bcf8b commit 4d838c4
Show file tree
Hide file tree
Showing 9 changed files with 634 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 1.19.23 (Unreleased)
## 1.19.24 (Unreleased)
## 1.19.23 (November 27, 2023)
NOTES:
- ADD SCS Security ip.

## 1.19.22 (November 24, 2023)
NOTES:
- The routing table now supports import.
Expand Down
1 change: 1 addition & 0 deletions baiducloud/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
BaiduCloudTestResourceTypeNameRdsSecurityIp = BaiduCloudTestResourceTypeName + "-" + "rds-security-ip"
BaiduCloudTestResourceTypeNameRouteRule = BaiduCloudTestResourceTypeName + "-" + "route-rule"
BaiduCloudTestResourceTypeNameScs = BaiduCloudTestResourceTypeName + "-" + "scs"
BaiduCloudTestResourceTypeNameScsSecurityIp = BaiduCloudTestResourceTypeName + "-" + "scs-security-ip"
BaiduCloudTestResourceTypeNameSecurityGroup = BaiduCloudTestResourceTypeName + "-" + "security-group"
BaiduCloudTestResourceTypeNameSecurityGroupRule = BaiduCloudTestResourceTypeName + "-" + "security-group-rule"
BaiduCloudTestResourceTypeNameSnapshot = BaiduCloudTestResourceTypeName + "-" + "snapshot"
Expand Down
98 changes: 98 additions & 0 deletions baiducloud/data_source_baiducloud_scs_security_ips.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Use this data source to query SCS security ips.
Example Usage
```hcl
data "baiducloud_scs_security_ips" "default" {
instance_id = "scs-xxxxx"
}
output "security_ips" {
value = "${data.baiducloud_scs.default.security_ips}"
}
```
*/
package baiducloud

import (
"github.com/baidubce/bce-sdk-go/services/scs"
"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 dataSourceBaiduCloudScsSecurityIps() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudScsSecurityIpsRead,

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 dataSourceBaiduCloudScsSecurityIpsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)

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

raw, err := client.WithScsClient(func(scsClient *scs.Client) (interface{}, error) {
return scsClient.GetSecurityIp(instanceID)
})

addDebug(action, raw)

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

securityIpsResult, _ := raw.(*scs.GetSecurityIpResult)
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_scs_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_scs_security_ips", action, BCESDKGoERROR)
}
}
return nil
}
66 changes: 66 additions & 0 deletions baiducloud/data_source_baiducloud_scs_security_ips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package baiducloud

import (
"fmt"
"testing"

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

const (
testAccScsSecurityIpDataSourceName = "data.baiducloud_scs_security_ips.default"
testAccScsSecurityIpDataSourceAttrKeyPrefix = "security_ips.0."
)

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

func testAccScsSecurityIpDataSourceConfig() string {
return fmt.Sprintf(`
data "baiducloud_scs_security_ips" "default" {
instance_id = "scs-bj-hzsywuljybfy"
}
`)
}

func testAccScsSecurityIpFullConfig() string {
return fmt.Sprintf(`
resource "baiducloud_scs" "default" {
instance_name = "scs-test"
billing = {
payment_timing = "Postpaid"
}
purchase_count = 1
port = 6379
engine_version = "3.2"
node_type = "cache.n1.micro"
cluster_type = "master_slave"
replication_num = 1
shard_num = 1
proxy_num = 0
}
data "baiducloud_scs_security_ips" "default" {
instance_id = baiducloud_scs.default.id
}
`)
}
2 changes: 2 additions & 0 deletions baiducloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func Provider() terraform.ResourceProvider {
"baiducloud_cfc_function": dataSourceBaiduCloudCFCFunction(),
"baiducloud_scs_specs": dataSourceBaiduCloudScsSpecs(),
"baiducloud_scss": dataSourceBaiduCloudScss(),
"baiducloud_scs_security_ips": dataSourceBaiduCloudScsSecurityIps(),
"baiducloud_cce_versions": dataSourceBaiduCloudCceKubernetesVersion(),
"baiducloud_cce_container_net": dataSourceBaiduCloudCceContainerNet(),
"baiducloud_cce_cluster_nodes": dataSourceBaiduCloudCCEClusterNodes(),
Expand Down Expand Up @@ -271,6 +272,7 @@ func Provider() terraform.ResourceProvider {
"baiducloud_cfc_version": resourceBaiduCloudCFCVersion(),
"baiducloud_cfc_trigger": resourceBaiduCloudCFCTrigger(),
"baiducloud_scs": resourceBaiduCloudScs(),
"baiducloud_scs_security_ip": resourceBaiduCloudScsSecurityIp(),
"baiducloud_cce_cluster": resourceBaiduCloudCCECluster(),
"baiducloud_ccev2_cluster": resourceBaiduCloudCCEv2Cluster(),
"baiducloud_ccev2_instance": resourceBaiduCloudCCEv2Instance(),
Expand Down
Loading

0 comments on commit 4d838c4

Please sign in to comment.