Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VPNaaS Endpoint Group #163

Merged
merged 2 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_cs_cluster_v1": resourceCsClusterV1(),
"huaweicloud_cs_peering_connect_v1": resourceCsPeeringConnectV1(),
"huaweicloud_vpnaas_service_v2": resourceVpnServiceV2(),
"huaweicloud_vpnaas_endpoint_group_v2": resourceVpnEndpointGroupV2(),
},

ConfigureFunc: configureProvider,
Expand Down
291 changes: 291 additions & 0 deletions huaweicloud/resource_huaweicloud_vpnaas_endpoint_group_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
package huaweicloud

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/vpnaas/endpointgroups"
)

func resourceVpnEndpointGroupV2() *schema.Resource {
return &schema.Resource{
Create: resourceVpnEndpointGroupV2Create,
Read: resourceVpnEndpointGroupV2Read,
Update: resourceVpnEndpointGroupV2Update,
Delete: resourceVpnEndpointGroupV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"endpoints": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"value_specs": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}

func resourceVpnEndpointGroupV2Create(d *schema.ResourceData, meta interface{}) error {

config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

var createOpts endpointgroups.CreateOptsBuilder

endpointType := resourceVpnEndpointGroupV2EndpointType(d.Get("type").(string))
v := d.Get("endpoints").([]interface{})
endpoints := make([]string, len(v))
for i, v := range v {
endpoints[i] = v.(string)
}

createOpts = VpnEndpointGroupCreateOpts{
endpointgroups.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
TenantID: d.Get("tenant_id").(string),
Endpoints: endpoints,
Type: endpointType,
},
MapValueSpecs(d),
}

log.Printf("[DEBUG] Create group: %#v", createOpts)

group, err := endpointgroups.Create(networkingClient, createOpts).Extract()
if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING_CREATE"},
Target: []string{"ACTIVE"},
Refresh: waitForEndpointGroupCreation(networkingClient, group.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 0,
MinTimeout: 2 * time.Second,
}
_, err = stateConf.WaitForState()

if err != nil {
return err
}

log.Printf("[DEBUG] EndpointGroup created: %#v", group)

d.SetId(group.ID)

return resourceVpnEndpointGroupV2Read(d, meta)
}

func resourceVpnEndpointGroupV2Read(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Retrieve information about group: %s", d.Id())

config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

group, err := endpointgroups.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "group")
}

log.Printf("[DEBUG] Read HuaweiCloud Endpoint EndpointGroup %s: %#v", d.Id(), group)

d.Set("name", group.Name)
d.Set("description", group.Description)
d.Set("tenant_id", group.TenantID)
d.Set("type", group.Type)
d.Set("endpoints", group.Endpoints)
d.Set("region", GetRegion(d, config))

return nil
}

func resourceVpnEndpointGroupV2Update(d *schema.ResourceData, meta interface{}) error {

config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

opts := endpointgroups.UpdateOpts{}

var hasChange bool

if d.HasChange("name") {
name := d.Get("name").(string)
opts.Name = &name
hasChange = true
}

if d.HasChange("description") {
description := d.Get("description").(string)
opts.Description = &description
hasChange = true
}

var updateOpts endpointgroups.UpdateOptsBuilder
updateOpts = opts

log.Printf("[DEBUG] Updating endpoint group with id %s: %#v", d.Id(), updateOpts)

if hasChange {
group, err := endpointgroups.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING_UPDATE"},
Target: []string{"UPDATED"},
Refresh: waitForEndpointGroupUpdate(networkingClient, group.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 0,
MinTimeout: 2 * time.Second,
}
_, err = stateConf.WaitForState()

if err != nil {
return err
}

log.Printf("[DEBUG] Updated group with id %s", d.Id())
}

return resourceVpnEndpointGroupV2Read(d, meta)
}

func resourceVpnEndpointGroupV2Delete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Destroy group: %s", d.Id())

config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

err = endpointgroups.Delete(networkingClient, d.Id()).Err

if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{"DELETING"},
Target: []string{"DELETED"},
Refresh: waitForEndpointGroupDeletion(networkingClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 0,
MinTimeout: 2 * time.Second,
}

_, err = stateConf.WaitForState()

return err
}

func waitForEndpointGroupDeletion(networkingClient *golangsdk.ServiceClient, id string) resource.StateRefreshFunc {

return func() (interface{}, string, error) {
group, err := endpointgroups.Get(networkingClient, id).Extract()
log.Printf("[DEBUG] Got group %s => %#v", id, group)

if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[DEBUG] EndpointGroup %s is actually deleted", id)
return "", "DELETED", nil
}
return nil, "", fmt.Errorf("Unexpected error: %s", err)
}

log.Printf("[DEBUG] EndpointGroup %s deletion is pending", id)
return group, "DELETING", nil
}
}

func waitForEndpointGroupCreation(networkingClient *golangsdk.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
group, err := endpointgroups.Get(networkingClient, id).Extract()
if err != nil {
return "", "PENDING_CREATE", nil
}
return group, "ACTIVE", nil
}
}

func waitForEndpointGroupUpdate(networkingClient *golangsdk.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
group, err := endpointgroups.Get(networkingClient, id).Extract()
if err != nil {
return "", "PENDING_UPDATE", nil
}
return group, "UPDATED", nil
}
}

func resourceVpnEndpointGroupV2EndpointType(epType string) endpointgroups.EndpointType {
var et endpointgroups.EndpointType
switch epType {
case "subnet":
et = endpointgroups.TypeSubnet
case "cidr":
et = endpointgroups.TypeCIDR
case "vlan":
et = endpointgroups.TypeVLAN
case "router":
et = endpointgroups.TypeRouter
case "network":
et = endpointgroups.TypeNetwork
}
return et
}
Loading