-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add label related resourcs and datasources (#166)
- Loading branch information
Showing
43 changed files
with
4,824 additions
and
786 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"] | ||
} | ||
` |
Oops, something went wrong.