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

[DCAAS] refactoring opentelekomcloud_dc_virtual_gateway_v2, deprecation opentelekomcloud_dc_endpoint_group_v2 #2405

Merged
merged 2 commits into from
Jan 9, 2024
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
4 changes: 4 additions & 0 deletions docs/resources/dс_endpoint_group_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ subcategory: "Direct Connect (DCaaS)"
Up-to-date reference of API arguments for Direct Connect Endpoint Group you can get at
[Official Docs Portal](https://docs.otc.t-systems.com/direct-connect/api-ref/apis/direct_connect_endpoint_group/index.html).

~>
opentelekomcloud_dc_endpoint_group_v2 is no longer provided. Impossible to update assigned endpoint group.
Please use `opentelekomcloud_dc_virtual_gateway_v2` with `local_ep_group` block instead.

## Example Usage

```hcl
Expand Down
32 changes: 15 additions & 17 deletions docs/resources/dс_virtual_gateway_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ Up-to-date reference of API arguments for Direct Connect Virtual Gateway you can
## Example Usage

```hcl
data "opentelekomcloud_identity_project_v3" "project" {
name = "eu-de_project_1"
}

resource "opentelekomcloud_dc_endpoint_group_v2" "dc_endpoint_group" {
name = "tf_acc_eg_1"
type = "cidr"
endpoints = ["10.2.0.0/24", "10.3.0.0/24"]
description = "first"
project_id = data.opentelekomcloud_identity_project_v3.project.id
}

resource "opentelekomcloud_dc_virtual_gateway_v2" "vgw_1" {
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
name = "my_virtual_gateway"
description = "acc test"
local_ep_group_id = opentelekomcloud_dc_endpoint_group_v2.dc_endpoint_group.id
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
name = "%s"
description = "acc test"
local_ep_group {
name = "tf_eg_1"
endpoints = ["10.2.0.0/24", "10.3.0.0/24"]
description = "first"
}
}
```

Expand All @@ -34,7 +26,12 @@ resource "opentelekomcloud_dc_virtual_gateway_v2" "vgw_1" {
The following arguments are supported:

* `vpc_id` (String, Required, ForceNew) - Specifies the ID of the VPC to be accessed.
* `local_ep_group_id` (String, Required) - Specifies the ID of the local endpoint group that records CIDR blocks of the VPC subnets.
* `local_ep_group` (String, Required, List) - Specifies the the local endpoint group that records CIDR blocks of the VPC subnets.
The `local_ep_group` block supports:
* `name` (String, Optional) - Specifies the name of the Direct Connect endpoint group.
* `description` (String, Optional) - Provides supplementary information about the Direct Connect endpoint group.
* `endpoints` (List, Required) - Specifies the list of the endpoints in a Direct Connect endpoint group.
* `type` (String, Required, ForceNew) - Specifies the type of the Direct Connect endpoints. The value can only be `cidr`. Default value: `cidr`.
* `name` (String, Required) - Specifies the virtual gateway name.
* `description` (String, Optional) - Provides supplementary information about the virtual gateway.
* `asn` (Int, Optional, ForceNew) - Specifies the BGP ASN of the virtual gateway.
Expand All @@ -48,6 +45,7 @@ The following attributes are exported:
* `id` - ID of the virtual gateway.
* `status` - Virtual gateway status.
* `project_id` - Project id.
* `local_ep_group_id` - ID of the local endpoint group that records CIDR blocks of the VPC subnets.

## Import

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func TestDirectConnectVirtualGatewayV2Resource_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(vg, "asn"),
resource.TestCheckResourceAttrSet(vg, "status"),
resource.TestCheckResourceAttrSet(vg, "local_ep_group_id"),
resource.TestCheckResourceAttr(vg, "local_ep_group.0.type", "cidr"),
resource.TestCheckResourceAttr(vg, "local_ep_group.0.endpoints.#", "1"),
),
},
{
Expand All @@ -45,6 +47,8 @@ func TestDirectConnectVirtualGatewayV2Resource_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(vg, "asn"),
resource.TestCheckResourceAttrSet(vg, "status"),
resource.TestCheckResourceAttrSet(vg, "local_ep_group_id"),
resource.TestCheckResourceAttr(vg, "local_ep_group.0.type", "cidr"),
resource.TestCheckResourceAttr(vg, "local_ep_group.0.endpoints.#", "2"),
),
},
{
Expand Down Expand Up @@ -118,19 +122,15 @@ func testAccVirtualGatewayV2_basic(name string) string {

%s

resource "opentelekomcloud_dc_endpoint_group_v2" "dc_endpoint_group" {
name = "tf_acc_eg_1"
type = "cidr"
endpoints = ["10.2.0.0/24", "10.3.0.0/24"]
description = "first"
project_id = data.opentelekomcloud_identity_project_v3.project.id
}

resource "opentelekomcloud_dc_virtual_gateway_v2" "vgw_1" {
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
name = "%s"
description = "acc test"
local_ep_group_id = opentelekomcloud_dc_endpoint_group_v2.dc_endpoint_group.id
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
name = "%s"
description = "acc test"
local_ep_group {
name = "tf_acc_eg_1"
endpoints = ["10.2.0.0/24"]
description = "first"
}
}
`, common.DataSourceSubnet, common.DataSourceProject, name)
}
Expand All @@ -141,27 +141,15 @@ func testAccVirtualGatewayV2_update(name string) string {

%s

resource "opentelekomcloud_dc_endpoint_group_v2" "dc_endpoint_group" {
name = "tf_acc_eg_1"
type = "cidr"
endpoints = ["10.2.0.0/24", "10.3.0.0/24"]
description = "first"
project_id = data.opentelekomcloud_identity_project_v3.project.id
}

resource "opentelekomcloud_dc_endpoint_group_v2" "dc_endpoint_group_new" {
name = "tf_acc_eg_1"
type = "cidr"
endpoints = ["10.20.0.0/24", "10.30.0.0/24"]
description = "first"
project_id = data.opentelekomcloud_identity_project_v3.project.id
}

resource "opentelekomcloud_dc_virtual_gateway_v2" "vgw_1" {
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
name = "%s"
description = "acc test updated"
local_ep_group_id = opentelekomcloud_dc_endpoint_group_v2.dc_endpoint_group_new.id
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
name = "%s"
description = "acc test updated"
local_ep_group {
name = "tf_acc_eg_1"
endpoints = ["10.2.0.0/24", "10.3.0.0/24"]
description = "first"
}
}
`, common.DataSourceSubnet, common.DataSourceProject, name)
}
2 changes: 2 additions & 0 deletions opentelekomcloud/services/dcaas/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ const (
errCreateClient = "error creating OpenTelekomCloud DCaaSv2 client: %w"
keyClientV2 = "dcaas-v2-client"
)

const egDeprecated = "This resource is not longer supported. Please use opentelekomcloud_dc_virtual_gateway_v2 with local_ep_group block instead."
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func ResourceDCEndpointGroupV2() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

DeprecationMessage: egDeprecated,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
Expand Down Expand Up @@ -79,7 +81,7 @@ func resourceDCEndpointGroupV2Create(ctx context.Context, d *schema.ResourceData
Name: d.Get("name").(string),
TenantId: d.Get("project_id").(string),
Description: d.Get("description").(string),
Endpoints: GetEndpoints(d),
Endpoints: GetEndpointsValues(d),
Type: d.Get("type").(string),
}
log.Printf("[DEBUG] DC endpoint group V2 createOpts: %+v", createOpts)
Expand Down Expand Up @@ -139,7 +141,7 @@ func resourceDCEndpointGroupV2Delete(ctx context.Context, d *schema.ResourceData
return nil
}

func GetEndpoints(d *schema.ResourceData) []string {
func GetEndpointsValues(d *schema.ResourceData) []string {
endpoints := make([]string, 0)
for _, val := range d.Get("endpoints").([]interface{}) {
endpoints = append(endpoints, val.(string))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package dcaas

import (
"context"
"log"
"regexp"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
dceg "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/dc-endpoint-group"
virtual_gateway "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/virtual-gateway"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
Expand All @@ -32,9 +34,35 @@ func ResourceVirtualGatewayV2() *schema.Resource {
Required: true,
ForceNew: true,
},
"local_ep_group_id": {
Type: schema.TypeString,
"local_ep_group": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"endpoints": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "cidr",
},
},
},
},
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -71,13 +99,45 @@ func ResourceVirtualGatewayV2() *schema.Resource {
Optional: true,
Computed: true,
},
"local_ep_group_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceEgCreate(client *golangsdk.ServiceClient, d *schema.ResourceData) (*dceg.DCEndpointGroup, error) {
var createOpts = dceg.CreateOpts{}
projectId := d.Get("project_id").(string)
if projectId == "" {
projectId = client.ProjectID
}
egRaw := d.Get("local_ep_group").([]interface{})
if len(egRaw) == 1 {
rawMap := egRaw[0].(map[string]interface{})
createOpts = dceg.CreateOpts{
Name: rawMap["name"].(string),
TenantId: projectId,
Description: rawMap["description"].(string),
Endpoints: GetEndpoints(rawMap["endpoints"].([]interface{})),
Type: rawMap["type"].(string),
}
}
eg, err := dceg.Create(client, createOpts)
return eg, err
}

func GetEndpoints(e []interface{}) []string {
endpoints := make([]string, 0)
for _, val := range e {
endpoints = append(endpoints, val.(string))
}
return endpoints
}

func resourceVirtualGatewayV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
Expand All @@ -88,9 +148,14 @@ func resourceVirtualGatewayV2Create(ctx context.Context, d *schema.ResourceData,
return fmterr.Errorf(errCreateClient, err)
}

eg, err := resourceEgCreate(client, d)
if err != nil {
return fmterr.Errorf("error creating DC endpoint group: %s", err)
}

opts := virtual_gateway.CreateOpts{
VpcId: d.Get("vpc_id").(string),
LocalEndpointGroupId: d.Get("local_ep_group_id").(string),
LocalEndpointGroupId: eg.ID,
Name: d.Get("name").(string),
Description: d.Get("description").(string),
BgpAsn: d.Get("asn").(int),
Expand Down Expand Up @@ -122,6 +187,20 @@ func resourceVirtualGatewayV2Read(ctx context.Context, d *schema.ResourceData, m
return common.CheckDeletedDiag(d, err, "virtual gateway")
}

eg, err := dceg.Get(client, vg.LocalEPGroupID)
if err != nil {
return fmterr.Errorf("error reading DC endpoint group: %s", err)
}
log.Printf("[DEBUG] DC endpoint group V2 read: %+v", eg)
group := []map[string]interface{}{
{
"name": eg.Name,
"description": eg.Description,
"endpoints": eg.Endpoints,
"type": eg.Type,
},
}

mErr := multierror.Append(nil,
d.Set("vpc_id", vg.VPCID),
d.Set("local_ep_group_id", vg.LocalEPGroupID),
Expand All @@ -131,6 +210,7 @@ func resourceVirtualGatewayV2Read(ctx context.Context, d *schema.ResourceData, m
d.Set("device_id", vg.DeviceID),
d.Set("redundant_device_id", vg.RedundantDeviceID),
d.Set("project_id", vg.TenantID),
d.Set("local_ep_group", group),
d.Set("status", vg.Status),
)

Expand All @@ -149,10 +229,34 @@ func resourceVirtualGatewayV2Update(ctx context.Context, d *schema.ResourceData,
return fmterr.Errorf(errCreateClient, err)
}

if d.HasChange("local_ep_group") {
newEg, err := resourceEgCreate(client, d)
if err != nil {
return fmterr.Errorf("error creating new DC endpoint group: %s", err)
}

vg, err := virtual_gateway.Get(client, d.Id())
if err != nil {
return common.CheckDeletedDiag(d, err, "virtual gateway")
}

opts := virtual_gateway.UpdateOpts{
LocalEndpointGroupId: newEg.ID,
}
err = virtual_gateway.Update(client, d.Id(), opts)
if err != nil {
return diag.Errorf("error updating opentelekomcloud virtual gateway (%s): %s", d.Id(), err)
}

err = dceg.Delete(client, vg.LocalEPGroupID)
if err != nil {
return fmterr.Errorf("error deleting old DC endpoint group: %s", err)
}
}

opts := virtual_gateway.UpdateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
LocalEndpointGroupId: d.Get("local_ep_group_id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
err = virtual_gateway.Update(client, d.Id(), opts)
if err != nil {
Expand All @@ -172,10 +276,20 @@ func resourceVirtualGatewayV2Delete(ctx context.Context, d *schema.ResourceData,
return fmterr.Errorf(errCreateClient, err)
}

vg, err := virtual_gateway.Get(client, d.Id())
if err != nil {
return common.CheckDeletedDiag(d, err, "virtual gateway")
}

err = virtual_gateway.Delete(client, d.Id())
if err != nil {
return diag.Errorf("error deleting opentelekomcloud virtual gateway (%s): %s", d.Id(), err)
}

err = dceg.Delete(client, vg.LocalEPGroupID)
if err != nil {
return fmterr.Errorf("error deleting DC endpoint group: %s", err)
}

return nil
}