Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support import in cce addon #1115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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