Skip to content

Commit

Permalink
add resource cce addon
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 committed Aug 26, 2020
1 parent fa465f9 commit ded9190
Show file tree
Hide file tree
Showing 16 changed files with 683 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/terraform-plugin-sdk v1.13.0
github.com/huaweicloud/golangsdk v0.0.0-20200825034233-150d13f435c6
github.com/huaweicloud/golangsdk v0.0.0-20200826004603-97095eb9d28e
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/mitchellh/go-homedir v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20200825034233-150d13f435c6 h1:YK3rhJit+LshGVxD5weCVd1HYCj1SGV8lNJEX9XfHpA=
github.com/huaweicloud/golangsdk v0.0.0-20200825034233-150d13f435c6/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200826004603-97095eb9d28e h1:lpFMrN/V5grfGpITmlFHDCx+i1Mkd31C6bNHjN1Lo4Q=
github.com/huaweicloud/golangsdk v0.0.0-20200826004603-97095eb9d28e/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
7 changes: 7 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ func (c *Config) cceV3Client(region string) (*golangsdk.ServiceClient, error) {
})
}

func (c *Config) cceAddonV3Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewCCEAddonV3(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getHwEndpointType(),
})
}

func (c *Config) cciV1Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.CCIV1(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Expand Down
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_as_policy": resourceASPolicy(),
"huaweicloud_cce_cluster": resourceCCEClusterV3(),
"huaweicloud_cce_node": resourceCCENodeV3(),
"huaweicloud_cce_addon": resourceCCEAddonV3(),
"huaweicloud_cci_network": resourceCCINetworkV1(),
"huaweicloud_cdm_cluster": resourceCdmClusterV1(),
"huaweicloud_cdn_domain": resourceCdnDomainV1(),
Expand Down
205 changes: 205 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_addon_v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
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,
ForceNew: true,
},
"addon_version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"addon_template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: 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
}
}
137 changes: 137 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_addon_v3_test.go
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))
}
Loading

0 comments on commit ded9190

Please sign in to comment.