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 SWR organization resource #1046

Merged
merged 3 commits into from
Apr 9, 2021
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
50 changes: 50 additions & 0 deletions docs/resources/swr_organization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
subcategory: "SoftWare Repository for Container (SWR)"
---

# huaweicloud\_swr\_organization

Manages a SWR organization resource within HuaweiCloud.

## Example Usage

```hcl
resource "huaweicloud_swr_organization" "test" {
name = "terraform-test"
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource. If omitted, the provider-level region will be used. Changing this creates a new resource.

* `name` - (Required, String, ForceNew) Specifies the name of the organization. The organization name must be globally unique.


## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - ID of the organization.

* `creator` - The creator user name of the organization.

* `permission` - The permission of the organization, the value can be Manage, Write, and Read.

* `login_server` - The URL that can be used to log into the container registry.


## Timeouts
This resource provides the following timeouts configuration options:
- `create` - Default is 10 minute.
- `delete` - Default is 10 minute.

## Import

Organizations can be imported using the `name`, e.g.

```
$ terraform import huaweicloud_swr_organization.test org-name
```
4 changes: 4 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ func (c *Config) FgsV2Client(region string) (*golangsdk.ServiceClient, error) {
return c.NewServiceClient("fgsv2", region)
}

func (c *Config) SwrV2Client(region string) (*golangsdk.ServiceClient, error) {
return c.NewServiceClient("swr", region)
}

// ********** client for Storage **********
func (c *Config) BlockStorageV2Client(region string) (*golangsdk.ServiceClient, error) {
return c.NewServiceClient("volumev2", region)
Expand Down
5 changes: 5 additions & 0 deletions huaweicloud/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ var allServiceCatalog = map[string]ServiceCatalog{
Name: "functiongraph",
Version: "v2",
},
"swr": {
Name: "swr-api",
Version: "v2",
WithOutProjectID: true,
},
niuzhenguo marked this conversation as resolved.
Show resolved Hide resolved

// ******* catalog for storage ******
"volumev2": {
Expand Down
10 changes: 10 additions & 0 deletions huaweicloud/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,16 @@ func TestAccServiceEndpoints_Compute(t *testing.T) {
expectedURL = fmt.Sprintf("https://functiongraph.%s.%s/v2/%s/", HW_REGION_NAME, config.Cloud, config.TenantID)
actualURL = serviceClient.ResourceBaseURL()
compareURL(expectedURL, actualURL, "fgs", "v2", t)

// test for swrV2Client
serviceClient, err = nil, nil
serviceClient, err = config.SwrV2Client(HW_REGION_NAME)
if err != nil {
t.Fatalf("Error creating HuaweiCloud swr v2 client: %s", err)
}
expectedURL = fmt.Sprintf("https://swr-api.%s.%s/v2/", HW_REGION_NAME, config.Cloud)
actualURL = serviceClient.ResourceBaseURL()
compareURL(expectedURL, actualURL, "swr", "v2", t)
}

// TestAccServiceEndpoints_Storage test for the endpoints of the clients used in storage
Expand Down
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_sfs_turbo": ResourceSFSTurbo(),
"huaweicloud_smn_topic": resourceTopic(),
"huaweicloud_smn_subscription": resourceSubscription(),
"huaweicloud_swr_organization": resourceSWROrganization(),
"huaweicloud_vbs_backup": resourceVBSBackupV2(),
"huaweicloud_vbs_backup_policy": resourceVBSBackupPolicyV2(),
"huaweicloud_vpc": ResourceVirtualPrivateCloudV1(),
Expand Down
131 changes: 131 additions & 0 deletions huaweicloud/resource_huaweicloud_swr_organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package huaweicloud

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/swr/v2/namespaces"
)

func resourceSWROrganization() *schema.Resource {
return &schema.Resource{
Create: resourceSWROrganizationCreate,
Read: resourceSWROrganizationRead,
Delete: resourceSWROrganizationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

//request and response parameters
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"creator": {
Type: schema.TypeString,
Computed: true,
},
"permission": {
niuzhenguo marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
},
"login_server": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceSWROrganizationCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
swrClient, err := config.SwrV2Client(GetRegion(d, config))

if err != nil {
return fmt.Errorf("Unable to create HuaweiCloud SWR client : %s", err)
}

name := d.Get("name").(string)
createOpts := namespaces.CreateOpts{
Namespace: name,
}

err = namespaces.Create(swrClient, createOpts).ExtractErr()

if err != nil {
return fmt.Errorf("Error creating HuaweiCloud SWR Organization: %s", err)
}

d.SetId(name)

return resourceSWROrganizationRead(d, meta)
}

func resourceSWROrganizationRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
swrClient, err := config.SwrV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud SWR client: %s", err)
}

n, err := namespaces.Get(swrClient, d.Id()).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving HuaweiCloud SWR: %s", err)
}

permission := "Unknown"
switch n.Auth {
case 7:
permission = "Manage"
case 3:
permission = "Write"
case 1:
permission = "Read"
}

d.Set("region", GetRegion(d, config))
d.Set("name", n.Name)
d.Set("creator", n.CreatorName)
d.Set("permission", permission)

login := fmt.Sprintf("swr.%s.%s", GetRegion(d, config), config.Cloud)
d.Set("login_server", login)

return nil
}

func resourceSWROrganizationDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
swrClient, err := config.SwrV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud SWR Client: %s", err)
}

err = namespaces.Delete(swrClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud SWR Organization: %s", err)
}

d.SetId("")
return nil
}
103 changes: 103 additions & 0 deletions huaweicloud/resource_huaweicloud_swr_organization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package huaweicloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

"github.com/huaweicloud/golangsdk/openstack/swr/v2/namespaces"
)

func TestAccSWROrganization_basic(t *testing.T) {
var org namespaces.Namespace

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "huaweicloud_swr_organization.test"
loginServer := fmt.Sprintf("swr.%s.myhuaweicloud.com", HW_REGION_NAME)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSWROrganizationDestroy,
Steps: []resource.TestStep{
{
Config: testAccSWROrganization_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSWROrganizationExists(resourceName, &org),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "permission", "Manage"),
resource.TestCheckResourceAttr(resourceName, "login_server", loginServer),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckSWROrganizationDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
swrClient, err := config.SwrV2Client(HW_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud SWR client: %s", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "huaweicloud_swr_organization" {
continue
}

_, err := namespaces.Get(swrClient, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("SWR organization still exists")
}
}

return nil
}

func testAccCheckSWROrganizationExists(n string, org *namespaces.Namespace) 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 := testAccProvider.Meta().(*Config)
swrClient, err := config.SwrV2Client(HW_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud SWR client: %s", err)
}

found, err := namespaces.Get(swrClient, rs.Primary.ID).Extract()
if err != nil {
return err
}

if found.Name != rs.Primary.ID {
return fmt.Errorf("SWR organization not found")
}

*org = *found

return nil
}
}

func testAccSWROrganization_basic(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_swr_organization" "test" {
name = "%s"
}
`, rName)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading