-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa465f9
commit dbd0139
Showing
28 changed files
with
774 additions
and
716 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/huaweicloud/golangsdk" | ||
"github.com/huaweicloud/golangsdk/openstack/cce/v3/addons" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceCCEAddonV3() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCCEAddonV3Create, | ||
Read: resourceCCEAddonV3Read, | ||
Delete: resourceCCEAddonV3Delete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
Delete: schema.DefaultTimeout(3 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ // request and response parameters | ||
"cluster_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"version": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"template_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"kind": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"api_version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCCEAddonV3Create(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
cceClient, err := config.cceAddonV3Client(GetRegion(d, config)) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Unable to create HuaweiCloud CCE client : %s", err) | ||
} | ||
|
||
var cluster_id = d.Get("cluster_id").(string) | ||
|
||
createOpts := addons.CreateOpts{ | ||
Kind: "Addon", | ||
ApiVersion: "v3", | ||
Metadata: addons.CreateMetadata{ | ||
Anno: addons.Annotations{ | ||
AddonInstallType: "install", | ||
}, | ||
}, | ||
Spec: addons.RequestSpec{ | ||
Version: d.Get("version").(string), | ||
ClusterID: cluster_id, | ||
AddonTemplateName: d.Get("template_name").(string), | ||
Values: addons.Values{ | ||
Basic: map[string]string{}, | ||
}, | ||
}, | ||
} | ||
|
||
create, err := addons.Create(cceClient, createOpts, cluster_id).Extract() | ||
|
||
d.SetId(create.Metadata.Id) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud CCEAddon: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Waiting for HuaweiCloud CCEAddon (%s) to become available", create.Metadata.Id) | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"installing", "abnormal"}, | ||
Target: []string{"running"}, | ||
Refresh: waitForCCEAddonActive(cceClient, create.Metadata.Id, cluster_id), | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
Delay: 5 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud CCEAddon: %s", err) | ||
} | ||
|
||
return resourceCCEAddonV3Read(d, meta) | ||
} | ||
|
||
func resourceCCEAddonV3Read(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
cceClient, err := config.cceAddonV3Client(GetRegion(d, config)) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud CCE client: %s", err) | ||
} | ||
|
||
var cluster_id = d.Get("cluster_id").(string) | ||
|
||
n, err := addons.Get(cceClient, d.Id(), cluster_id).Extract() | ||
if err != nil { | ||
if _, ok := err.(golangsdk.ErrDefault404); ok { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error retrieving HuaweiCloud CCEAddon: %s", err) | ||
} | ||
|
||
d.Set("cluster_id", n.Spec.ClusterID) | ||
d.Set("version", n.Spec.Version) | ||
d.Set("template_name", n.Spec.AddonTemplateName) | ||
d.Set("name", n.Metadata.Name) | ||
d.Set("status", n.Status.Status) | ||
d.Set("description", n.Spec.Description) | ||
d.Set("kind", n.Kind) | ||
d.Set("api_version", n.ApiVersion) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCCEAddonV3Delete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
cceClient, err := config.cceAddonV3Client(GetRegion(d, config)) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud CCEAddon Client: %s", err) | ||
} | ||
|
||
var cluster_id = d.Get("cluster_id").(string) | ||
|
||
err = addons.Delete(cceClient, d.Id(), cluster_id).ExtractErr() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting HuaweiCloud CCE Addon: %s", err) | ||
} | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"Deleting", "Available", "Unavailable"}, | ||
Target: []string{"Deleted"}, | ||
Refresh: waitForCCEAddonDelete(cceClient, d.Id(), d.Get("cluster_id").(string)), | ||
Timeout: d.Timeout(schema.TimeoutDelete), | ||
Delay: 5 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error deleting HuaweiCloud CCE Addon: %s", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func waitForCCEAddonActive(cceAddonClient *golangsdk.ServiceClient, id, clusterID string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
n, err := addons.Get(cceAddonClient, id, clusterID).Extract() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return n, n.Status.Status, nil | ||
} | ||
} | ||
|
||
func waitForCCEAddonDelete(cceClient *golangsdk.ServiceClient, id, clusterID string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
log.Printf("[DEBUG] Attempting to delete HuaweiCloud CCE Addon %s.\n", id) | ||
|
||
r, err := addons.Get(cceClient, id, clusterID).Extract() | ||
|
||
if err != nil { | ||
if _, ok := err.(golangsdk.ErrDefault404); ok { | ||
log.Printf("[DEBUG] Successfully deleted HuaweiCloud CCE Addon %s", id) | ||
return r, "Deleted", nil | ||
} | ||
} | ||
if r.Status.Status == "Deleting" { | ||
return r, "Deleting", nil | ||
} | ||
log.Printf("[DEBUG] HuaweiCloud CCE Addon %s still available.\n", id) | ||
return r, "Available", 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,138 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
|
||
"github.com/huaweicloud/golangsdk/openstack/cce/v3/addons" | ||
) | ||
|
||
func TestAccCCEAddonV3_basic(t *testing.T) { | ||
var addon addons.Addon | ||
|
||
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5)) | ||
resourceName := "huaweicloud_cce_addon.test" | ||
clusterName := "huaweicloud_cce_cluster_v3.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCCEAddonV3Destroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCCEAddonV3_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCCEAddonV3Exists(resourceName, clusterName, &addon), | ||
resource.TestCheckResourceAttr(resourceName, "status", "running"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCCEAddonV3Destroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
cceClient, err := config.cceAddonV3Client(OS_REGION_NAME) | ||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud CCE Addon client: %s", err) | ||
} | ||
|
||
var clusterId string | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type == "huaweicloud_cce_cluster_v3" { | ||
clusterId = rs.Primary.ID | ||
} | ||
|
||
if rs.Type != "huaweicloud_cce_addon" { | ||
continue | ||
} | ||
|
||
if clusterId != "" { | ||
_, err := addons.Get(cceClient, rs.Primary.ID, clusterId).Extract() | ||
if err == nil { | ||
return fmt.Errorf("addon still exists") | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckCCEAddonV3Exists(n string, cluster string, addon *addons.Addon) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
c, ok := s.RootModule().Resources[cluster] | ||
if !ok { | ||
return fmt.Errorf("Cluster not found: %s", c) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
if c.Primary.ID == "" { | ||
return fmt.Errorf("Cluster id is not set") | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
cceClient, err := config.cceAddonV3Client(OS_REGION_NAME) | ||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud CCE Addon client: %s", err) | ||
} | ||
|
||
found, err := addons.Get(cceClient, rs.Primary.ID, c.Primary.ID).Extract() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if found.Metadata.Id != rs.Primary.ID { | ||
return fmt.Errorf("Addon not found") | ||
} | ||
|
||
*addon = *found | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCCEAddonV3_Base(rName string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "huaweicloud_cce_node_v3" "test" { | ||
cluster_id = huaweicloud_cce_cluster_v3.test.id | ||
name = "%s" | ||
flavor_id = "s6.large.2" | ||
availability_zone = data.huaweicloud_availability_zones.test.names[0] | ||
key_pair = huaweicloud_compute_keypair_v2.test.name | ||
root_volume { | ||
size = 40 | ||
volumetype = "SATA" | ||
} | ||
data_volumes { | ||
size = 100 | ||
volumetype = "SATA" | ||
} | ||
} | ||
`, testAccCCENodeV3_Base(rName), rName) | ||
} | ||
|
||
func testAccCCEAddonV3_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "huaweicloud_cce_addon" "test" { | ||
cluster_id = huaweicloud_cce_cluster_v3.test.id | ||
version = "1.0.3" | ||
template_name = "metrics-server" | ||
depends_on = [huaweicloud_cce_node_v3.test] | ||
} | ||
`, testAccCCEAddonV3_Base(rName)) | ||
} |
Oops, something went wrong.