Skip to content

Commit

Permalink
support import in cce add on (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 authored May 11, 2021
1 parent a78a3ea commit 2f2a385
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/resources/cce_addon.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
21 changes: 21 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_addon_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package huaweicloud
import (
"fmt"
"log"
"strings"
"time"

"github.com/huaweicloud/golangsdk"
Expand All @@ -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),
Expand Down Expand Up @@ -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 <cluster id>/<addon id>")
return nil, err
}

clusterID := parts[0]
addonID := parts[1]

d.SetId(addonID)
d.Set("cluster_id", clusterID)

return []*schema.ResourceData{d}, nil
}
24 changes: 24 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_addon_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func TestAccCCEAddonV3_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "status", "running"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccCCEAddonImportStateIdFunc(),
},
},
})
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2f2a385

Please sign in to comment.