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 27, 2020
1 parent fa465f9 commit dbd0139
Show file tree
Hide file tree
Showing 28 changed files with 774 additions and 716 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-20200827051055-27c9c7b6b988
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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ 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/huaweicloud/golangsdk v0.0.0-20200827051055-27c9c7b6b988 h1:a/lJW8PBLTcr1h062r1zKZBBB6Z23VteWcSnwF9JCds=
github.com/huaweicloud/golangsdk v0.0.0-20200827051055-27c9c7b6b988/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
215 changes: 215 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_addon_v3.go
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
}
}
138 changes: 138 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_addon_v3_test.go
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))
}
Loading

0 comments on commit dbd0139

Please sign in to comment.