-
Notifications
You must be signed in to change notification settings - Fork 162
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 31ccda0
Showing
14 changed files
with
637 additions
and
2 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,202 @@ | ||
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, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
Delete: schema.DefaultTimeout(3 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ // request and response parameters | ||
"authenticating_proxy_ca": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"cluster_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"addon_version": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"addon_template_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"region": { | ||
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) | ||
} | ||
authenticating_proxy := make(map[string]string) | ||
if hasFilledOpt(d, "authenticating_proxy_ca") { | ||
authenticating_proxy["ca"] = d.Get("authenticating_proxy_ca").(string) | ||
} | ||
createOpts := addons.CreateOpts{ | ||
Kind: "Addon", | ||
ApiVersion: "v3", | ||
Metadata: addons.CreateMetadata{ | ||
Anno: addons.Annotations{ | ||
AddonInstallType: "install", | ||
}, | ||
}, | ||
Spec: addons.RequestSpec{ | ||
Version: d.Get("addon_version").(string), | ||
ClusterID: d.Get("cluster_id").(string), | ||
AddonTemplateName: d.Get("addon_template_name").(string), | ||
Values: addons.Values{ | ||
Basic: map[string]string{}, | ||
}, | ||
}, | ||
} | ||
|
||
create, err := addons.Create(cceClient, createOpts, d.Get("cluster_id").(string)).Extract() | ||
|
||
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, d.Get("cluster_id").(string)), | ||
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) | ||
} | ||
d.SetId(create.Metadata.Id) | ||
|
||
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) | ||
} | ||
|
||
n, err := addons.Get(cceClient, d.Id(), d.Get("cluster_id").(string)).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("name", n.Metadata.Name) | ||
d.Set("status", n.Status.Status) | ||
d.Set("region", GetRegion(d, config)) | ||
|
||
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) | ||
} | ||
err = addons.Delete(cceClient, d.Id(), d.Get("cluster_id").(string)).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,137 @@ | ||
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_v3.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, "name", rName), | ||
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_v3" { | ||
continue | ||
} | ||
|
||
_, 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_v3" "test" { | ||
cluster_id = huaweicloud_cce_cluster_v3.test.id | ||
addon_version = "1.0.3" | ||
addon_template_name = "metrics-server" | ||
depends_on = [huaweicloud_cce_node_v3.test] | ||
} | ||
`, testAccCCEAddonV3_Base(rName)) | ||
} |
Oops, something went wrong.