Skip to content

Commit 7b6a299

Browse files
committed
[Feat] Add organiztion impl
1 parent c283942 commit 7b6a299

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "Software Repository for Container (SWR)"
3+
layout: "opentelekomcloud"
4+
page_title: "OpenTelekomCloud: opentelekomcloud_swr_organization_v2"
5+
sidebar_current: "docs-opentelekomcloud-data-source-swr-organization-v2"
6+
description: |-
7+
Get details of SWR Organizations within OpenTelekomCloud.
8+
---
9+
10+
Up-to-date reference of API arguments for SWR organization you can get at
11+
[documentation portal](https://docs.otc.t-systems.com/software-repository-container/api-ref/api)
12+
13+
# opentelekomcloud_swr_organization_v2
14+
15+
Get details of SWR organizations within Open Telekom Cloud.
16+
17+
## Example Usage
18+
19+
```hcl
20+
data opentelekomcloud_swr_organization_v2 org_1 {}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `name` - (Optional) The name of the SWR organization. Use this to filter organizations list.
28+
29+
## Attributes Reference
30+
31+
In addition to all arguments above, the following attributes are exported:
32+
33+
* `organizations` - List of organizations. The structure is documented below:
34+
* `name` - Organization name.
35+
* `organization_id` - Numeric ID of the organization.
36+
* `creator_name` - Username of the organization creator.
37+
* `auth` - User permission. The value can be `1`, `3`, or `7`. `7`: manage `3`: write `1`: read
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package swr
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
9+
)
10+
11+
func TestSwrOrganizationV2DS_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { common.TestAccPreCheck(t) },
14+
ProviderFactories: common.TestAccProviderFactories,
15+
CheckDestroy: testSwrOrganizationV2Destroy,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testSwrOrganizationV2DSBasic,
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttr(dataSourceOrgName, "organizations.0.auth", "7"),
21+
resource.TestCheckResourceAttrSet(dataSourceOrg2Name, "organizations.0.organization_id"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const dataSourceOrgName = "data.opentelekomcloud_swr_organization_v2.org_1"
29+
const dataSourceOrg2Name = "data.opentelekomcloud_swr_organization_v2.org_2"
30+
31+
var testSwrOrganizationV2DSBasic = fmt.Sprintf(
32+
`
33+
resource opentelekomcloud_swr_organization_v2 org {
34+
name = "%[1]s"
35+
}
36+
37+
data opentelekomcloud_swr_organization_v2 org_1 {
38+
depends_on = [opentelekomcloud_swr_organization_v2.org]
39+
name = "%[1]s"
40+
}
41+
42+
data opentelekomcloud_swr_organization_v2 org_2 {}
43+
`, name)

opentelekomcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ func Provider() *schema.Provider {
408408
"opentelekomcloud_swr_domain_v2": swr.DataSourceSwrDomainV2(),
409409
"opentelekomcloud_swr_domains_v2": swr.DataSourceSwrDomainsV2(),
410410
"opentelekomcloud_swr_repository_v2": swr.DataSourceSwrRepositoryV2(),
411+
"opentelekomcloud_swr_organization_v2": swr.DataSourceSwrOrganizationV2(),
411412
"opentelekomcloud_taurusdb_mysql_backups_v3": taurusdb.DataSourceTaurusDBV3MysqlBackups(),
412413
"opentelekomcloud_taurusdb_mysql_configuration_v3": taurusdb.DataSourceTaurusDBV3MysqlConfiguration(),
413414
"opentelekomcloud_taurusdb_mysql_configurations_v3": taurusdb.DataSourceTaurusDBV3MysqlConfigurations(),
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package swr
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/go-multierror"
7+
"github.com/hashicorp/go-uuid"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/swr/v2/organizations"
11+
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
12+
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr"
13+
)
14+
15+
func DataSourceSwrOrganizationV2() *schema.Resource {
16+
return &schema.Resource{
17+
ReadContext: dataSourceOrganizationRead,
18+
19+
Schema: map[string]*schema.Schema{
20+
"name": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
},
24+
"organizations": {
25+
Type: schema.TypeList,
26+
Computed: true,
27+
Elem: &schema.Resource{
28+
Schema: map[string]*schema.Schema{
29+
"name": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"organization_id": {
34+
Type: schema.TypeInt,
35+
Computed: true,
36+
},
37+
"creator_name": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"auth": {
42+
Type: schema.TypeInt,
43+
Computed: true,
44+
},
45+
},
46+
},
47+
},
48+
},
49+
}
50+
}
51+
52+
func dataSourceOrganizationRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
53+
config := meta.(*cfg.Config)
54+
client, err := config.SwrV2Client(config.GetRegion(d))
55+
if err != nil {
56+
return fmterr.Errorf(ClientError, err)
57+
}
58+
org, err := organizations.List(client, organizations.ListOpts{
59+
Namespace: d.Get("name").(string),
60+
})
61+
if err != nil {
62+
return fmterr.Errorf("error reading SWR organizations: %w", err)
63+
}
64+
65+
id, err := uuid.GenerateUUID()
66+
if err != nil {
67+
return diag.Errorf("unable to generate ID: %s", err)
68+
}
69+
d.SetId(id)
70+
71+
mErr := multierror.Append(
72+
d.Set("organizations", setOrganizations(org)),
73+
)
74+
if err := mErr.ErrorOrNil(); err != nil {
75+
return fmterr.Errorf("error setting SWR organization fields: %w", err)
76+
}
77+
return nil
78+
}
79+
80+
func setOrganizations(orgsInResp []organizations.Organization) []map[string]interface{} {
81+
var orgs []map[string]interface{}
82+
for _, orgInResp := range orgsInResp {
83+
org := map[string]interface{}{
84+
"name": orgInResp.Name,
85+
"organization_id": orgInResp.ID,
86+
"creator_name": orgInResp.CreatorName,
87+
"auth": orgInResp.Auth,
88+
}
89+
orgs = append(orgs, org)
90+
}
91+
return orgs
92+
}

releasenotes/notes/swr-add-data-sources-repository-0754ad4e83959bc0.yaml renamed to releasenotes/notes/swr-add-data-sources-repo-and-org-0754ad4e83959bc0.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
features:
33
- |
44
**[SWR]** Add new data source ``datasource/opentelekomcloud_swr_repository_v2`` (`#3322 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/3322>`_).
5+
- |
6+
**[SWR]** Add new data source ``datasource/opentelekomcloud_swr_organization_v2`` (`#3322 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/3322>`_).

0 commit comments

Comments
 (0)