diff --git a/docs/resources/cce_addon.md b/docs/resources/cce_addon.md index dd2ba924a7..c2137615ea 100644 --- a/docs/resources/cce_addon.md +++ b/docs/resources/cce_addon.md @@ -38,3 +38,11 @@ This resource provides the following timeouts configuration options: - `create` - Default is 10 minute. - `delete` - Default is 3 minute. +## Import + +CCE addon can be imported using the cluster ID and addon ID +separated by a slash, e.g.: + +``` +$ terraform import huaweicloud_cce_addon.my_addon bb6923e4-b16e-11eb-b0cd-0255ac101da1/c7ecb230-b16f-11eb-b3b6-0255ac1015a3 +``` diff --git a/huaweicloud/resource_huaweicloud_cce_addon_v3.go b/huaweicloud/resource_huaweicloud_cce_addon_v3.go index e01dee46cb..a6c2b27da2 100644 --- a/huaweicloud/resource_huaweicloud_cce_addon_v3.go +++ b/huaweicloud/resource_huaweicloud_cce_addon_v3.go @@ -3,6 +3,7 @@ package huaweicloud import ( "fmt" "log" + "strings" "time" "github.com/huaweicloud/golangsdk" @@ -19,6 +20,10 @@ func ResourceCCEAddonV3() *schema.Resource { Read: resourceCCEAddonV3Read, Delete: resourceCCEAddonV3Delete, + Importer: &schema.ResourceImporter{ + State: resourceCCEAddonV3Import, + }, + Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(10 * time.Minute), Delete: schema.DefaultTimeout(3 * time.Minute), @@ -252,3 +257,19 @@ func waitForCCEAddonDelete(cceClient *golangsdk.ServiceClient, id, clusterID str return r, "Available", nil } } + +func resourceCCEAddonV3Import(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.SplitN(d.Id(), "/", 2) + if len(parts) != 2 { + err := fmt.Errorf("Invalid format specified for CCE Addon. Format must be /") + return nil, err + } + + clusterID := parts[0] + addonID := parts[1] + + d.SetId(addonID) + d.Set("cluster_id", clusterID) + + return []*schema.ResourceData{d}, nil +} diff --git a/huaweicloud/resource_huaweicloud_cce_addon_v3_test.go b/huaweicloud/resource_huaweicloud_cce_addon_v3_test.go index 0c85501a71..07434d35ef 100644 --- a/huaweicloud/resource_huaweicloud_cce_addon_v3_test.go +++ b/huaweicloud/resource_huaweicloud_cce_addon_v3_test.go @@ -31,6 +31,12 @@ func TestAccCCEAddonV3_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "status", "running"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: testAccCCEAddonImportStateIdFunc(), + }, }, }) } @@ -102,6 +108,24 @@ func testAccCheckCCEAddonV3Exists(n string, cluster string, addon *addons.Addon) } } +func testAccCCEAddonImportStateIdFunc() resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + var clusterID string + var addonID string + for _, rs := range s.RootModule().Resources { + if rs.Type == "huaweicloud_cce_cluster" { + clusterID = rs.Primary.ID + } else if rs.Type == "huaweicloud_cce_addon" { + addonID = rs.Primary.ID + } + } + if clusterID == "" || addonID == "" { + return "", fmt.Errorf("resource not found: %s/%s", clusterID, addonID) + } + return fmt.Sprintf("%s/%s", clusterID, addonID), nil + } +} + func testAccCCEAddonV3_Base(rName string) string { return fmt.Sprintf(` %s