From d7153e6bb056703adc68414e24a9031e2954fcce Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Sat, 29 Feb 2020 16:42:28 +0800 Subject: [PATCH 1/3] Add huaweicloud_api_gateway_group resource and doc Signed-off-by: ShiChangkuo --- go.mod | 2 +- go.sum | 3 + huaweicloud/config.go | 7 + huaweicloud/provider.go | 1 + .../resource_huaweicloud_api_gateway_group.go | 124 ++++++++++++++++++ ...urce_huaweicloud_api_gateway_group_test.go | 106 +++++++++++++++ .../docs/r/api_gateway_group.html.markdown | 39 ++++++ website/huaweicloud.erb | 9 ++ 8 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 huaweicloud/resource_huaweicloud_api_gateway_group.go create mode 100644 huaweicloud/resource_huaweicloud_api_gateway_group_test.go create mode 100644 website/docs/r/api_gateway_group.html.markdown diff --git a/go.mod b/go.mod index 1b3bc43dc3..b800cf6b0b 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ require ( github.com/hashicorp/errwrap v1.0.0 github.com/hashicorp/go-cleanhttp v0.5.1 github.com/hashicorp/terraform-plugin-sdk v1.0.0 - github.com/huaweicloud/golangsdk v0.0.0-20200204080439-744c2b9d7746 + github.com/huaweicloud/golangsdk v0.0.0-20200229071744-cde13ac05a94 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 diff --git a/go.sum b/go.sum index 941e50bf7a..60be5dd0a9 100644 --- a/go.sum +++ b/go.sum @@ -127,6 +127,9 @@ github.com/huaweicloud/golangsdk v0.0.0-20191210113552-8f6bd7a6f1d8 h1:7dfGs0ENj github.com/huaweicloud/golangsdk v0.0.0-20191210113552-8f6bd7a6f1d8/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw= github.com/huaweicloud/golangsdk v0.0.0-20200204080439-744c2b9d7746 h1:c2HOmXuDF1VSiBdCZAPo6kbOSTt9Kh6jV87mmiegkE0= github.com/huaweicloud/golangsdk v0.0.0-20200204080439-744c2b9d7746/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw= +github.com/huaweicloud/golangsdk v0.0.0-20200226090834-31e7d87f9e61 h1:cHkhULuZneNT7GRwigfbRM8IXxLH4mNdDEUezszJ3go= +github.com/huaweicloud/golangsdk v0.0.0-20200229071744-cde13ac05a94 h1:3MKOAqfBneQCxf7+GMMql6n+JIvE+rmdgtjoM1IhDWc= +github.com/huaweicloud/golangsdk v0.0.0-20200229071744-cde13ac05a94/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/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= diff --git a/huaweicloud/config.go b/huaweicloud/config.go index 9e02ec8564..7933a20307 100644 --- a/huaweicloud/config.go +++ b/huaweicloud/config.go @@ -436,6 +436,13 @@ func (c *Config) newObjectStorageClient(region string) (*obs.ObsClient, error) { return obs.New(c.AccessKey, c.SecretKey, client.Endpoint) } +func (c *Config) apiGatewayV1Client(region string) (*golangsdk.ServiceClient, error) { + return huaweisdk.ApiGateWayV1(c.HwClient, golangsdk.EndpointOpts{ + Region: c.determineRegion(region), + Availability: c.getHwEndpointType(), + }) +} + func (c *Config) blockStorageV1Client(region string) (*golangsdk.ServiceClient, error) { return huaweisdk.NewBlockStorageV1(c.HwClient, golangsdk.EndpointOpts{ Region: c.determineRegion(region), diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 0c33a9479d..7d698f5fda 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -233,6 +233,7 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ + "huaweicloud_api_gateway_group": resourceAPIGatewayGroup(), "huaweicloud_blockstorage_volume_v2": resourceBlockStorageVolumeV2(), "huaweicloud_compute_instance_v2": resourceComputeInstanceV2(), "huaweicloud_compute_interface_attach_v2": resourceComputeInterfaceAttachV2(), diff --git a/huaweicloud/resource_huaweicloud_api_gateway_group.go b/huaweicloud/resource_huaweicloud_api_gateway_group.go new file mode 100644 index 0000000000..45b0530d06 --- /dev/null +++ b/huaweicloud/resource_huaweicloud_api_gateway_group.go @@ -0,0 +1,124 @@ +package huaweicloud + +import ( + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/huaweicloud/golangsdk/openstack/apigw/groups" +) + +func resourceAPIGatewayGroup() *schema.Resource { + return &schema.Resource{ + Create: resourceAPIGatewayGroupCreate, + Read: resourceAPIGatewayGroupRead, + Update: resourceAPIGatewayGroupUpdate, + Delete: resourceAPIGatewayGroupDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(10 * time.Minute), + Delete: schema.DefaultTimeout(10 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: false, + }, + "description": { + Type: schema.TypeString, + Optional: true, + ForceNew: false, + }, + "status": { + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func resourceAPIGatewayGroupCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + apigwClient, err := config.apiGatewayV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api gateway client: %s", err) + } + + createOpts := &groups.CreateOpts{ + Name: d.Get("name").(string), + Remark: d.Get("description").(string), + } + + log.Printf("[DEBUG] Create Options: %#v", createOpts) + v, err := groups.Create(apigwClient, createOpts).Extract() + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api group: %s", err) + } + + // Store the ID now + d.SetId(v.ID) + + return resourceAPIGatewayGroupRead(d, meta) +} + +func resourceAPIGatewayGroupRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + apigwClient, err := config.apiGatewayV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api gateway client: %s", err) + } + + v, err := groups.Get(apigwClient, d.Id()).Extract() + if err != nil { + return fmt.Errorf("Error retrieving HuaweiCloud api group: %s", err) + } + + log.Printf("[DEBUG] Retrieved api group %s: %+v", d.Id(), v) + + d.Set("name", v.Name) + d.Set("description", v.Remark) + d.Set("status", v.Status) + + return nil +} + +func resourceAPIGatewayGroupUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + apigwClient, err := config.apiGatewayV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api gateway client: %s", err) + } + + updateOpts := groups.UpdateOpts{ + Name: d.Get("name").(string), + Remark: d.Get("description").(string), + } + + _, err = groups.Update(apigwClient, d.Id(), updateOpts).Extract() + if err != nil { + return fmt.Errorf("Error updating HuaweiCloud api group: %s", err) + } + + return resourceAPIGatewayGroupRead(d, meta) +} + +func resourceAPIGatewayGroupDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + apigwClient, err := config.apiGatewayV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api gateway client: %s", err) + } + + if err := groups.Delete(apigwClient, d.Id()).ExtractErr(); err != nil { + return CheckDeleted(d, err, "api groups") + } + + d.SetId("") + return nil +} diff --git a/huaweicloud/resource_huaweicloud_api_gateway_group_test.go b/huaweicloud/resource_huaweicloud_api_gateway_group_test.go new file mode 100644 index 0000000000..e15bca2ae2 --- /dev/null +++ b/huaweicloud/resource_huaweicloud_api_gateway_group_test.go @@ -0,0 +1,106 @@ +package huaweicloud + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/huaweicloud/golangsdk/openstack/apigw/groups" +) + +func TestAccApiGatewayGroup_basic(t *testing.T) { + var resName = "huaweicloud_api_gateway_group.acc_apigw_group" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckApiGatewayGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccApigwGroup_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckApiGatewayGroupExists(resName), + resource.TestCheckResourceAttr( + resName, "name", "acc_apigw_group_1"), + resource.TestCheckResourceAttr( + resName, "description", "created by acc test"), + ), + }, + { + Config: testAccApigwGroup_update, + Check: resource.ComposeTestCheckFunc( + testAccCheckApiGatewayGroupExists(resName), + resource.TestCheckResourceAttr( + resName, "name", "acc_apigw_group_update"), + resource.TestCheckResourceAttr( + resName, "description", "updated by acc test"), + ), + }, + }, + }) +} + +func testAccCheckApiGatewayGroupDestroy(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + apigwClient, err := config.apiGatewayV1Client(OS_REGION_NAME) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api gateway client: %s", err) + } + + for _, rs := range s.RootModule().Resources { + if rs.Type != "huaweicloud_api_gateway_group" { + continue + } + + _, err := groups.Get(apigwClient, rs.Primary.ID).Extract() + if err == nil { + return fmt.Errorf("api gateway group still exists") + } + } + + return nil +} + +func testAccCheckApiGatewayGroupExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Resource %s not found", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + apigwClient, err := config.apiGatewayV1Client(OS_REGION_NAME) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud api gateway client: %s", err) + } + + found, err := groups.Get(apigwClient, rs.Primary.ID).Extract() + if err != nil { + return err + } + + if found.ID != rs.Primary.ID { + return fmt.Errorf("apigateway group not found") + } + + return nil + } +} + +const testAccApigwGroup_basic = ` +resource "huaweicloud_api_gateway_group" "acc_apigw_group" { + name = "acc_apigw_group_1" + description = "created by acc test" +} +` +const testAccApigwGroup_update = ` +resource "huaweicloud_api_gateway_group" "acc_apigw_group" { + name = "acc_apigw_group_update" + description = "updated by acc test" +} +` diff --git a/website/docs/r/api_gateway_group.html.markdown b/website/docs/r/api_gateway_group.html.markdown new file mode 100644 index 0000000000..fb2517fc6f --- /dev/null +++ b/website/docs/r/api_gateway_group.html.markdown @@ -0,0 +1,39 @@ +--- +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_api_gateway_group" +sidebar_current: "docs-huaweicloud-resource-api-gateway-group" +description: |- + Provides an API gateway group resource. +--- + +# huaweicloud\_api\_gateway\_group + +Provides an API gateway group resource. + +## Example Usage + +```hcl +resource "huaweicloud_api_gateway_group" "apigw_group" { + name = "apigw_group" + description = "your descpiption" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the API group. An API group name consists of 3–64 characters, + starting with a letter. Only letters, digits, and underscores (_) are allowed. + +* `description` - (Optional) Specifies the description of the API group. + The description cannot exceed 255 characters. + +## Attributes Reference + +The following attributes are exported: + +* `id` - ID of the API group. +* `status` - Status of the API group. +* `name` - See Argument Reference above. +* `description` - See Argument Reference above. \ No newline at end of file diff --git a/website/huaweicloud.erb b/website/huaweicloud.erb index 51136daf0c..b04426976d 100644 --- a/website/huaweicloud.erb +++ b/website/huaweicloud.erb @@ -130,6 +130,15 @@ + > + API Gateway Resources + + + > Auto Scaling Resources