-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DCAAS] added new resource dc_endpoint_group (#2361)
[DCAAS] added new resource dc_endpoint_group Summary of the Pull Request PR Checklist Refers to: #2215 Tests added/passed. Documentation updated. Schema updated. Release notes added. Acceptance Steps Performed === RUN TestDCEndpointGroupV2Resource_basic --- PASS: TestDCEndpointGroupV2Resource_basic (31.38s) PASS Process finished with the exit code 0 Reviewed-by: Anton Sidelnikov Reviewed-by: Artem Lifshits
- Loading branch information
1 parent
49869c8
commit 51e05fa
Showing
7 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
subcategory: "Direct Connect (DCaaS)" | ||
--- | ||
# opentelekomcloud_dc_endpoint_group_v2 (Resource) | ||
|
||
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). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "opentelekomcloud_identity_project_v3" "project_1" { | ||
name = "eu-de_project_1" | ||
} | ||
resource "opentelekomcloud_dc_endpoint_group_v2" "dc_endpoint_group" { | ||
name = "ep-1" | ||
type = "cidr" | ||
endpoints = ["10.2.0.0/24", "10.3.0.0/24"] | ||
description = "first" | ||
project_id = data.opentelekomcloud_identity_project_v3.project_1.id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` (String, Optional, ForceNew) - Specifies the name of the Direct Connect endpoint group. | ||
* `project_id` (String, Required, ForceNew) - Specifies the project ID. | ||
* `description` (String, Optional, ForceNew) - Provides supplementary information about the Direct Connect endpoint group. | ||
* `endpoints` (List, required, ForceNew) - 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. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - ID of the endpoint group. | ||
|
||
## Import | ||
|
||
Direct Connect Endpoint Group can be imported using `id`, e.g. | ||
|
||
```sh | ||
$ terraform import opentelekomcloud_dc_endpoint_group_v2.eg <id> | ||
``` |
110 changes: 110 additions & 0 deletions
110
opentelekomcloud/acceptance/dcaas/resource_opentelekomcloud_dc_endpoint_group_v2_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package dcaas | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
golangsdk "github.com/opentelekomcloud/gophertelekomcloud" | ||
dcep "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/dc-endpoint-group" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" | ||
) | ||
|
||
const dceg = "opentelekomcloud_dc_endpoint_group_v2.dc_endpoint_group" | ||
|
||
func TestDCEndpointGroupV2Resource_basic(t *testing.T) { | ||
var group dcep.DCEndpointGroup | ||
DCegName := fmt.Sprintf("dceg-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { common.TestAccPreCheck(t) }, | ||
ProviderFactories: common.TestAccProviderFactories, | ||
CheckDestroy: testAccCheckDCegV2Destroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDCegV2Resource_basic(DCegName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDCegV2Exists(dceg, &group), | ||
resource.TestCheckResourceAttr(dceg, "description", "first"), | ||
resource.TestCheckResourceAttrSet(dceg, "id"), | ||
), | ||
}, | ||
{ | ||
ResourceName: dceg, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckDCegV2Exists(n string, group *dcep.DCEndpointGroup) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("no ID is set") | ||
} | ||
|
||
config := common.TestAccProvider.Meta().(*cfg.Config) | ||
client, err := config.DCaaSV2Client(env.OS_REGION_NAME) | ||
if err != nil { | ||
return fmt.Errorf("error creating OpenTelekomCloud DCaasV2 client: %s", err) | ||
} | ||
|
||
found, err := dcep.Get(client, rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
if found.ID != rs.Primary.ID { | ||
return fmt.Errorf("DC endpoint group not found") | ||
} | ||
group = found | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckDCegV2Destroy(s *terraform.State) error { | ||
config := common.TestAccProvider.Meta().(*cfg.Config) | ||
client, err := config.DCaaSV2Client(env.OS_REGION_NAME) | ||
if err != nil { | ||
return fmt.Errorf("error creating OpenTelekomCloud DCaasV2 client: %s", err) | ||
} | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "opentelekomcloud_dc_endpoint_group_v2" { | ||
continue | ||
} | ||
|
||
dceg, _ := dcep.Get(client, rs.Primary.ID) | ||
if dceg != nil { | ||
return fmt.Errorf("DC endpoint group still exists") | ||
} | ||
var errDefault404 golangsdk.ErrDefault404 | ||
if !errors.As(err, &errDefault404) { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccDCegV2Resource_basic(dcegName string) string { | ||
return fmt.Sprintf(` | ||
resource "opentelekomcloud_dc_endpoint_group_v2" "dc_endpoint_group" { | ||
name = "%s" | ||
type = "cidr" | ||
endpoints = ["10.2.0.0/24", "10.3.0.0/24"] | ||
description = "first" | ||
project_id = "%s" | ||
} | ||
`, dcegName, env.OS_PROJECT_ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
opentelekomcloud/services/dcaas/resource_opentelekomcloud_dc_endpoint_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package dcaas | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
golangsdk "github.com/opentelekomcloud/gophertelekomcloud" | ||
dceg "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/dc-endpoint-group" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr" | ||
) | ||
|
||
func ResourceDCEndpointGroupV2() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceDCEndpointGroupV2Create, | ||
DeleteContext: resourceDCEndpointGroupV2Delete, | ||
ReadContext: resourceDCEndpointGroupV2Read, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
Delete: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"project_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"endpoints": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
ForceNew: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDCEndpointGroupV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := common.ClientFromCtx(ctx, keyClientV2, func() (*golangsdk.ServiceClient, error) { | ||
return config.DCaaSV2Client(config.GetRegion(d)) | ||
}) | ||
if err != nil { | ||
return fmterr.Errorf(errCreateClient, err) | ||
} | ||
|
||
createOpts := dceg.CreateOpts{ | ||
Name: d.Get("name").(string), | ||
TenantId: d.Get("project_id").(string), | ||
Description: d.Get("description").(string), | ||
Endpoints: GetEndpoints(d), | ||
Type: d.Get("type").(string), | ||
} | ||
log.Printf("[DEBUG] DC endpoint group V2 createOpts: %+v", createOpts) | ||
|
||
eg, err := dceg.Create(client, createOpts) | ||
if err != nil { | ||
return fmterr.Errorf("error creating DC endpoint group: %s", err) | ||
} | ||
d.SetId(eg.ID) | ||
log.Printf("[DEBUG] DC endpoint group V2 created: %+v", eg) | ||
return resourceDCEndpointGroupV2Read(ctx, d, meta) | ||
} | ||
|
||
func resourceDCEndpointGroupV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := common.ClientFromCtx(ctx, keyClientV2, func() (*golangsdk.ServiceClient, error) { | ||
return config.DCaaSV2Client(config.GetRegion(d)) | ||
}) | ||
if err != nil { | ||
return fmterr.Errorf(errCreateClient, err) | ||
} | ||
|
||
eg, err := dceg.Get(client, d.Id()) | ||
if err != nil { | ||
return fmterr.Errorf("error reading DC endpoint group: %s", err) | ||
} | ||
log.Printf("[DEBUG] DC endpoint group V2 read: %+v", eg) | ||
|
||
mErr := multierror.Append( | ||
d.Set("name", eg.Name), | ||
d.Set("project_id", eg.TenantId), | ||
d.Set("description", eg.Description), | ||
d.Set("endpoints", eg.Endpoints), | ||
d.Set("type", eg.Type), | ||
) | ||
if err := mErr.ErrorOrNil(); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceDCEndpointGroupV2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := common.ClientFromCtx(ctx, keyClientV2, func() (*golangsdk.ServiceClient, error) { | ||
return config.DCaaSV2Client(config.GetRegion(d)) | ||
}) | ||
if err != nil { | ||
return fmterr.Errorf(errCreateClient, err) | ||
} | ||
|
||
err = dceg.Delete(client, d.Id()) | ||
if err != nil { | ||
return fmterr.Errorf("error deleting DC endpoint group: %s", err) | ||
} | ||
log.Printf("[DEBUG] DC endpoint group V2 deleted: %s", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func GetEndpoints(d *schema.ResourceData) []string { | ||
endpoints := make([]string, 0) | ||
for _, val := range d.Get("endpoints").([]interface{}) { | ||
endpoints = append(endpoints, val.(string)) | ||
} | ||
return endpoints | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
features: | ||
| | ||
**[DCAAS]** Add new resource ``resource/opentelekomcloud_dc_endpoint_group_v2`` (`#2361 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2361>`_) |