-
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.
[SWR] Add
opentelekomcloud_swr_domain_v2
resource (#1127)
[SWR] Add `opentelekomcloud_swr_domain_v2` resource Summary of the Pull Request Implement opentelekomcloud_swr_domain_v2 resource Part of: #623 PR Checklist Refers to: #623 Tests added/passed. Documentation updated. Schema updated. Acceptance Steps Performed === RUN TestSwrDomainV2Basic --- PASS: TestSwrDomainV2Basic (50.09s) PASS Process finished with the exit code 0 Reviewed-by: Anton Kachurin <katchuring@gmail.com> Reviewed-by: Anton Sidelnikov <None> Reviewed-by: None <None>
- Loading branch information
Showing
6 changed files
with
332 additions
and
5 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,67 @@ | ||
--- | ||
subcategory: "Software Repository for Container (SWR)" | ||
--- | ||
|
||
# opentelekomcloud_swr_domain_v2 | ||
|
||
Manages SWRv2 image sharing domain. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "access_domain" { } | ||
resource opentelekomcloud_swr_organization_v2 org_1 { | ||
name = "organization_1" | ||
} | ||
resource opentelekomcloud_swr_repository_v2 repo_1 { | ||
organization = opentelekomcloud_swr_organization_v2.org_1.name | ||
name = "repository_1" | ||
description = "Test repository" | ||
category = "linux" | ||
is_public = false | ||
} | ||
resource opentelekomcloud_swr_domain_v2 domain_1 { | ||
organization = opentelekomcloud_swr_organization_v2.org_1.name | ||
repository = opentelekomcloud_swr_organization_v2.repo_1.name | ||
access_domain = var.access_domain | ||
permission = "read" | ||
deadline = "forever" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `organization` - (Required) The name of the repository organization. | ||
|
||
* `repository` - (Required) The name of the repository. | ||
|
||
* `access_domain` - (Required) The name of the domain for image sharing. | ||
|
||
-> `access_domain` should be an existing OTC domain. | ||
|
||
* `permission` - (Required) Permission to be granted. Currently, only the `read` permission is supported. | ||
|
||
* `deadline` - (Required) End date of image sharing (UTC). When the value is set to `forever`, | ||
the image will be permanently available for the domain. The validity period is calculated by day. | ||
The shared images expire at `00:00:00` on the day after the end date. | ||
|
||
* `description` - (Optional) Specifies SWR domain description. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `creator_id` - Username ID of the domain creator. | ||
|
||
* `creator_name` - Username of the domain creator. | ||
|
||
* `created` - Indicates the creation time. | ||
|
||
* `updated` - Indicates the domain when was last updated. | ||
|
||
* `status` - Indicates the domain is valid (`true`) or expired (`false`). |
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
86 changes: 86 additions & 0 deletions
86
opentelekomcloud/acceptance/swr/resource_opentelekomcloud_swr_domain_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,86 @@ | ||
package swr | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/opentelekomcloud/gophertelekomcloud/openstack/swr/v2/domains" | ||
"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" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/swr" | ||
) | ||
|
||
func TestSwrDomainV2Basic(t *testing.T) { | ||
domainToShare := os.Getenv("OS_DOMAIN_NAME_2") | ||
if domainToShare == "" { | ||
t.Skip("OS_DOMAIN_NAME_2 is empty") | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { common.TestAccPreCheck(t) }, | ||
ProviderFactories: common.TestAccProviderFactories, | ||
CheckDestroy: testSwrDomainV2Destroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testSwrDomainV2Basic(name, domainToShare), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceDomainName, "permission", "read"), | ||
resource.TestCheckResourceAttr(resourceDomainName, "access_domain", domainToShare), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testSwrDomainV2Destroy(s *terraform.State) error { | ||
config := common.TestAccProvider.Meta().(*cfg.Config) | ||
client, err := config.SwrV2Client(env.OS_REGION_NAME) | ||
if err != nil { | ||
return fmt.Errorf(swr.SwrClientError, err) | ||
} | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "opentelekomcloud_swr_domain_v2" { | ||
continue | ||
} | ||
|
||
_, err := domains.Get(client, rs.Primary.Attributes["organization"], rs.Primary.Attributes["repository"], rs.Primary.ID).Extract() | ||
if err == nil { | ||
return fmt.Errorf("SWR domain still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const ( | ||
resourceDomainName = "opentelekomcloud_swr_domain_v2.domain_1" | ||
) | ||
|
||
func testSwrDomainV2Basic(name, domainToShare string) string { | ||
return fmt.Sprintf(` | ||
resource opentelekomcloud_swr_organization_v2 org_1 { | ||
name = "%[1]s" | ||
} | ||
resource opentelekomcloud_swr_repository_v2 repo_1 { | ||
organization = opentelekomcloud_swr_organization_v2.org_1.name | ||
name = "%[1]s" | ||
description = "Test repository" | ||
category = "linux" | ||
is_public = false | ||
} | ||
resource opentelekomcloud_swr_domain_v2 domain_1 { | ||
organization = opentelekomcloud_swr_organization_v2.org_1.name | ||
repository = opentelekomcloud_swr_repository_v2.repo_1.name | ||
access_domain = "%[2]s" | ||
permission = "read" | ||
deadline = "forever" | ||
} | ||
`, name, domainToShare) | ||
} |
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
175 changes: 175 additions & 0 deletions
175
opentelekomcloud/services/swr/resource_opentelekomcloud_swr_domain_v2.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,175 @@ | ||
package swr | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"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" | ||
"github.com/opentelekomcloud/gophertelekomcloud/openstack/swr/v2/domains" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr" | ||
) | ||
|
||
func ResourceSwrDomainV2() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceSwrDomainCreate, | ||
ReadContext: resourceSwrDomainRead, | ||
UpdateContext: resourceSwrDomainUpdate, | ||
DeleteContext: resourceSwrDomainDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Default: schema.DefaultTimeout(2 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"organization": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"access_domain": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"permission": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"read", | ||
}, false), | ||
}, | ||
"deadline": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"creator_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"creator_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"updated": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func repository(d *schema.ResourceData) string { | ||
return d.Get("repository").(string) | ||
} | ||
|
||
func resourceSwrDomainCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := config.SwrV2Client(config.GetRegion(d)) | ||
if err != nil { | ||
return fmterr.Errorf(SwrClientError, err) | ||
} | ||
|
||
opts := domains.CreateOpts{ | ||
AccessDomain: d.Get("access_domain").(string), | ||
Permit: d.Get("permission").(string), | ||
Deadline: d.Get("deadline").(string), | ||
Description: d.Get("description").(string), | ||
} | ||
|
||
err = domains.Create(client, organization(d), repository(d), opts).ExtractErr() | ||
if err != nil { | ||
return fmterr.Errorf("error creating domain: %w", err) | ||
} | ||
d.SetId(opts.AccessDomain) | ||
|
||
return resourceSwrDomainRead(ctx, d, meta) | ||
} | ||
|
||
func resourceSwrDomainRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := config.SwrV2Client(config.GetRegion(d)) | ||
if err != nil { | ||
return fmterr.Errorf(SwrClientError, err) | ||
} | ||
|
||
domain, err := domains.Get(client, organization(d), repository(d), d.Id()).Extract() | ||
if err != nil { | ||
return fmterr.Errorf("error reading domain: %w", err) | ||
} | ||
|
||
mErr := multierror.Append( | ||
d.Set("access_domain", strings.ToUpper(domain.AccessDomain)), | ||
d.Set("repository", domain.Repository), | ||
d.Set("organization", domain.Organization), | ||
d.Set("description", domain.Description), | ||
d.Set("status", domain.Status), | ||
d.Set("permission", domain.Permit), | ||
d.Set("created", domain.Created), | ||
d.Set("updated", domain.Updated), | ||
d.Set("creator_id", domain.CreatorID), | ||
d.Set("creator_name", domain.CreatorName), | ||
) | ||
if err := mErr.ErrorOrNil(); err != nil { | ||
return fmterr.Errorf("error setting resource fields: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceSwrDomainUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := config.SwrV2Client(config.GetRegion(d)) | ||
if err != nil { | ||
return fmterr.Errorf(SwrClientError, err) | ||
} | ||
|
||
opts := domains.UpdateOpts{ | ||
Permit: d.Get("permission").(string), | ||
Deadline: d.Get("deadline").(string), | ||
Description: d.Get("description").(string), | ||
} | ||
|
||
err = domains.Update(client, organization(d), repository(d), d.Id(), opts).ExtractErr() | ||
if err != nil { | ||
return fmterr.Errorf("error updating domain: %w", err) | ||
} | ||
|
||
return resourceSwrDomainRead(ctx, d, meta) | ||
} | ||
|
||
func resourceSwrDomainDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*cfg.Config) | ||
client, err := config.SwrV2Client(config.GetRegion(d)) | ||
if err != nil { | ||
return fmterr.Errorf(SwrClientError, err) | ||
} | ||
|
||
err = domains.Delete(client, organization(d), repository(d), d.Id()).ExtractErr() | ||
if err != nil { | ||
fmterr.Errorf("error deleting domain: %w", err) | ||
} | ||
|
||
return nil | ||
} |