Skip to content

Commit

Permalink
add label related resourcs and datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
wangrzneu committed Apr 24, 2024
1 parent 82d104b commit 84cdbe0
Show file tree
Hide file tree
Showing 43 changed files with 4,824 additions and 786 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v1.39.0 (2024-04-24)

FEATURES:

* **New Resource:** `ucloud_label`.
* **New Resource:** `ucloud_label_attachment`.
* **New Datasource:** `ucloud_labels`.
* **New Datasource:** `ucloud_label_resources`.

## v1.38.8 (2024-03-16)

FEATURES:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/mitchellh/mapstructure v1.1.2
github.com/pkg/errors v0.8.0
github.com/tidwall/gjson v1.16.0
github.com/ucloud/ucloud-sdk-go v0.22.5
github.com/ucloud/ucloud-sdk-go v0.22.17
)

require (
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/ucloud/ucloud-sdk-go v0.22.5 h1:GIltVwMDUqQj4iPL/emsZAMhEYWjLTwZqpOxdkdDrM8=
github.com/ucloud/ucloud-sdk-go v0.22.5/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.22.15 h1:JzD4GhwwtdN+QfA6p+JQoOLDywW7dHkqrUgTSnAbNGA=
github.com/ucloud/ucloud-sdk-go v0.22.15/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.22.17 h1:EFn+GxVKS5Tj8hIPie3qL6Zgk25fmWcHqJ06K8wl+Qo=
github.com/ucloud/ucloud-sdk-go v0.22.17/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
2 changes: 2 additions & 0 deletions ucloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ucloud/ucloud-sdk-go/services/uads"

"github.com/ucloud/ucloud-sdk-go/services/iam"
"github.com/ucloud/ucloud-sdk-go/services/label"
"github.com/ucloud/ucloud-sdk-go/services/udb"
"github.com/ucloud/ucloud-sdk-go/services/udisk"
"github.com/ucloud/ucloud-sdk-go/services/udpn"
Expand Down Expand Up @@ -48,6 +49,7 @@ type UCloudClient struct {
uk8sconn *uk8s.UK8SClient
uadsconn *uads.UADSClient
iamconn *iam.IAMClient
labelconn *label.LabelClient
uphostconn *uphost.UPHostClient
// private services
pumemconn *pumem.UMemClient
Expand Down
3 changes: 3 additions & 0 deletions ucloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/ucloud/ucloud-sdk-go/services/iam"
"github.com/ucloud/ucloud-sdk-go/services/label"
"github.com/ucloud/ucloud-sdk-go/services/uads"
"github.com/ucloud/ucloud-sdk-go/services/uphost"

Expand Down Expand Up @@ -155,6 +156,7 @@ func (c *Config) Client() (*UCloudClient, error) {
client.cubeconn = cube.NewClient(&cfg, &cred)
client.uadsconn = uads.NewClient(&cfg, &cred)
client.iamconn = iam.NewClient(&cfg, &cred)
client.labelconn = label.NewClient(&cfg, &cred)

// initialize client connections for private usage
client.pumemconn = pumem.NewClient(&cfg, &cred)
Expand Down Expand Up @@ -185,6 +187,7 @@ func (c *Config) Client() (*UCloudClient, error) {
client.uk8sconn.AddHttpRequestHandler(cloudShellCredHandler)
client.uadsconn.AddHttpRequestHandler(cloudShellCredHandler)
client.iamconn.AddHttpRequestHandler(cloudShellCredHandler)
client.labelconn.AddHttpRequestHandler(cloudShellCredHandler)
client.uphostconn.AddHttpRequestHandler(cloudShellCredHandler)
client.genericClient.AddHttpRequestHandler(cloudShellCredHandler)
}
Expand Down
119 changes: 119 additions & 0 deletions ucloud/data_source_ucloud_label_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package ucloud

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/ucloud/ucloud-sdk-go/services/label"
"github.com/ucloud/ucloud-sdk-go/ucloud"
)

func dataSourceUCloudLabelResources() *schema.Resource {
return &schema.Resource{
Read: dataSourceUCloudLabelResourcesRead,
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
"resource_types": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"project_ids": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
},
"resources": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceUCloudLabelResourcesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*UCloudClient)
conn := client.labelconn
limit := 100
offset := 0
resourcesData := make([]map[string]interface{}, 0)
ids := make([]string, 0)
for {
listResourcesReq := conn.NewListResourcesByLabelsRequest()
listResourcesReq.ResourceTypes = interfaceSliceToStringSlice(d.Get("resource_types").([]interface{}))
listResourcesReq.ProjectIds = interfaceSliceToStringSlice(d.Get("project_ids").([]interface{}))
listResourcesReq.Limit = ucloud.Int(limit)
listResourcesReq.Offset = ucloud.Int(offset)
listResourcesReq.Labels = []label.ListResourcesByLabelsParamLabels{
{
Key: ucloud.String(d.Get("key").(string)),
Value: ucloud.String(d.Get("value").(string)),
},
}
listResourcesResp, err := conn.ListResourcesByLabels(listResourcesReq)
if err != nil {
return fmt.Errorf("error on reading resources list, %s", err)
}

if len(listResourcesResp.Resources) < 1 {
break
}
for _, resource := range listResourcesResp.Resources {
ids = append(ids, resource.ResourceId)
resourcesData = append(resourcesData, map[string]interface{}{
"id": resource.ResourceId,
"name": resource.ResourceName,
"type": resource.ResourceType,
})
}
if len(listResourcesResp.Resources) < limit {
break
}
offset = offset + limit
}
d.SetId(hashStringArray(ids))
d.Set("total_count", len(resourcesData))
if err := d.Set("resources", resourcesData); err != nil {
return err
}

if outputFile, ok := d.GetOk("output_file"); ok && outputFile.(string) != "" {
writeToFile(outputFile.(string), resourcesData)
}
return nil
}
69 changes: 69 additions & 0 deletions ucloud/data_source_ucloud_label_resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ucloud

import (
"testing"

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

func TestAccUCloudLabelResourcesDataSource_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataLabelResourcesConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckIDExists("data.ucloud_labels.foo"),
resource.TestCheckResourceAttr("data.ucloud_label_resources.foo", "resources.#", "1"),
resource.TestCheckResourceAttr("data.ucloud_label_resources.foo", "resources.0.name", "tf-acc-vip-basic"),
resource.TestCheckResourceAttr("data.ucloud_label_resources.foo", "resources.0.type", "vip"),
),
},
},
})
}

const testAccDataLabelResourcesConfig = `
resource "ucloud_vpc" "foo" {
name = "tf-acc-vip"
tag = "tf-acc"
cidr_blocks = ["192.168.0.0/16"]
}
resource "ucloud_subnet" "foo" {
name = "tf-acc-vip"
tag = "tf-acc"
cidr_block = "192.168.1.0/24"
vpc_id = "${ucloud_vpc.foo.id}"
}
resource "ucloud_vip" "foo" {
vpc_id = "${ucloud_vpc.foo.id}"
subnet_id = "${ucloud_subnet.foo.id}"
name = "tf-acc-vip-basic"
remark = "test"
tag = "tf-acc"
}
resource "ucloud_label" "foo" {
key = "tf-acc-label-key"
value = "tf-acc-label-value"
}
resource "ucloud_label_attachment" "foo" {
key = "${ucloud_label.foo.key}"
value = "${ucloud_label.foo.value}"
resource = "${ucloud_vip.foo.id}"
}
data "ucloud_labels" "foo" {
key_regex = "^${ucloud_label_attachment.foo.key}$"
}
data "ucloud_label_resources" "foo" {
key = "${ucloud_label_attachment.foo.key}"
value = "${ucloud_label_attachment.foo.value}"
resource_types = ["vip"]
project_ids = ["${data.ucloud_labels.foo.labels.0.projects.0.id}"]
}
`
Loading

0 comments on commit 84cdbe0

Please sign in to comment.