Skip to content

Commit

Permalink
[CCE] Add possibility to import nodepools (#1082)
Browse files Browse the repository at this point in the history
[CCE] Add possibility to import nodepools

Summary of the Pull Request
Add possibility to import resource/opentelekomcloud_cce_node_pool_v3
Closes: #1040
PR Checklist

 Refers to: #1040
 Tests added/passed.
 Documentation updated.

Acceptance Steps Performed
Basic
=== RUN   TestAccCCENodePoolsV3_basic
--- PASS: TestAccCCENodePoolsV3_basic (832.48s)
=== RUN   TestAccCCENodePoolsV3_randomAZ
--- PASS: TestAccCCENodePoolsV3_randomAZ (694.29s)
PASS

Process finished with the exit code 0

Import
=== RUN   TestAccCCENodePoolV3ImportBasic
--- PASS: TestAccCCENodePoolV3ImportBasic (696.66s)
PASS

Process finished with the exit code 0

Reviewed-by: None <None>
Reviewed-by: Irina Pereiaslavskaia <None>
Reviewed-by: Polina Gubina <None>
  • Loading branch information
lego963 committed May 25, 2021
1 parent ece1e60 commit b3553a3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/resources/cce_node_pool_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,11 @@ This resource provides the following timeouts configuration options:
- `create` - Default is 30 minutes.
- `update` - Default is 30 minutes.
- `delete` - Default is 30 minutes.

## Import

CCE NodePool can be imported using the `cluster_id/node_pool_id`, e.g.

```sh
terraform import opentelekomcloud_cce_node_pool_v3.pool_1 14a80bc7-c12c-4fe0-a38a-cb77eeac9bd6/89c60255-9bd6-460c-822a-e2b959ede9d2
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package acceptance

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
)

func TestAccCCENodePoolV3ImportBasic(t *testing.T) {
resourceName := "opentelekomcloud_cce_node_pool_v3.node_pool"

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
Providers: common.TestAccProviders,
CheckDestroy: testAccCheckCCENodePoolV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCENodePoolV3_basic,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccCCENodePoolV3ImportStateIdFunc(),
ImportStateVerifyIgnore: []string{
"max_node_count", "min_node_count", "priority", "scale_down_cooldown_time",
},
},
},
})
}

func testAccCCENodePoolV3ImportStateIdFunc() resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var clusterID string
var nodePoolID string
for _, rs := range s.RootModule().Resources {
if rs.Type == "opentelekomcloud_cce_cluster_v3" {
clusterID = rs.Primary.ID
} else if rs.Type == "opentelekomcloud_cce_node_pool_v3" {
nodePoolID = rs.Primary.ID
}
}
if clusterID == "" || nodePoolID == "" {
return "", fmt.Errorf("resource not found: %s/%s", clusterID, nodePoolID)
}
return fmt.Sprintf("%s/%s", clusterID, nodePoolID), nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func ResourceCCENodePoolV3() *schema.Resource {
Default: schema.DefaultTimeout(15 * time.Minute),
},

Importer: &schema.ResourceImporter{
State: resourceCCENodePoolV3Import,
},

CustomizeDiff: common.MultipleCustomizeDiffs(
common.ValidateVolumeType("root_volume.*.volumetype"),
common.ValidateVolumeType("data_volumes.*.volumetype"),
Expand Down Expand Up @@ -538,3 +542,27 @@ func waitForCceNodePoolDelete(cceClient *golangsdk.ServiceClient, clusterId, nod
return r, r.Status.Phase, nil
}
}

func resourceCCENodePoolV3Import(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 NodePool. Format must be <cluster id>/<nodepool id>")
return nil, err
}
clusterID := parts[0]
nodePool := parts[1]

d.SetId(nodePool)
if err := d.Set("cluster_id", clusterID); err != nil {
return nil, err
}

results := make([]*schema.ResourceData, 1)
if err := resourceCCENodePoolV3Read(d, meta); err != nil {
return nil, fmt.Errorf("error reading opentelekomcloud_cce_node_pool_v3 %s: %w", d.Id(), err)
}

results[0] = d

return results, nil
}

0 comments on commit b3553a3

Please sign in to comment.