Skip to content

Commit f31543e

Browse files
authored
feat(registry): add support for project in namespace (#607)
1 parent 68ee162 commit f31543e

10 files changed

+1676
-58
lines changed

docs/data-sources/registry_image.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ data "scaleway_registry_image" "my_image" {
3737

3838
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the image is associated with.
3939

40+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the image is associated with.
41+
4042
## Attributes Reference
4143

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

docs/data-sources/registry_namespace.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ data "scaleway_registry_namespace" "my_namespace" {
3434

3535
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the namespace is associated with.
3636

37+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.
38+
3739
## Attributes Reference
3840

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

docs/resources/registry_namespace.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ description: |-
66

77
# scaleway_registry_namespace
88

9-
Creates and manages Scaleway Container Registry. For more information see [the documentation](https://developers.scaleway.com/en/products/registry/api/).
9+
Creates and manages Scaleway Container Registry.
10+
For more information see [the documentation](https://developers.scaleway.com/en/products/registry/api/).
1011

1112
## Examples
1213

@@ -24,28 +25,30 @@ resource "scaleway_registry_namespace" "main" {
2425

2526
The following arguments are supported:
2627

27-
- `name` - (Required) The unique name of the container registry namespace.
28+
- `name` - (Required) The unique name of the namespace.
2829

2930
~> **Important** Updates to `name` will recreate the namespace.
3031

31-
- `description` (Optional) The description of the container registry namespace.
32+
- `description` (Optional) The description of the namespace.
3233

33-
- `is_public` (Defaults to `false`) Whether or not the registry images stored in the namespace should be downloadable publicly (docker pull).
34+
- `is_public` (Defaults to `false`) Whether the images stored in the namespace should be downloadable publicly (docker pull).
3435

35-
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the container registry namespace should be created.
36+
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the namespace should be created.
3637

37-
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the registry is associated with.
38+
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the namespace is associated with.
39+
40+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.
3841

3942
## Attributes Reference
4043

41-
In addition to all arguments above, the following attibutes are exported:
44+
In addition to all arguments above, the following attributes are exported:
4245

4346
- `id` - The ID of the namespace
44-
- `endpoint` - Endpoint reachable by docker.
47+
- `endpoint` - Endpoint reachable by Docker.
4548

4649
## Import
4750

48-
Container Registry Namespace can be imported using the `{region}/{id}`, eg.
51+
Namespaces can be imported using the `{region}/{id}`, e.g.
4952

5053
```bash
5154
$ terraform import scaleway_registry_namespace.main fr-par/11111111-1111-1111-1111-111111111111

scaleway/data_source_registry_image.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func dataSourceScalewayRegistryImage() *schema.Resource {
5353
},
5454
"region": regionSchema(),
5555
"organization_id": organizationIDSchema(),
56+
"project_id": projectIDSchema(),
5657
},
5758
}
5859
}

scaleway/data_source_registry_image_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
)
1111

1212
func TestAccScalewayDataSourceRegistryImage_Basic(t *testing.T) {
13+
tt := NewTestTools(t)
14+
defer tt.Cleanup()
1315
ubuntuImageID := "4b5a47c0-6fbf-4388-8783-c07c28d3c2eb"
1416

1517
t.Skip("It is difficult to test this datasource as we cannot create registry images with Terraform.")
1618
resource.ParallelTest(t, resource.TestCase{
17-
PreCheck: func() { testAccPreCheck(t) },
18-
Providers: testAccProviders,
19-
CheckDestroy: testAccCheckScalewayRegistryNamespaceBetaDestroy,
19+
PreCheck: func() { testAccPreCheck(t) },
20+
ProviderFactories: tt.ProviderFactories,
21+
CheckDestroy: testAccCheckScalewayRegistryNamespaceBetaDestroy(tt),
2022
Steps: []resource.TestStep{
2123
{
2224
Config: `
@@ -55,8 +57,8 @@ func TestAccScalewayDataSourceRegistryImage_Basic(t *testing.T) {
5557
}
5658

5759
func testAccCheckScalewayRegistryImageExists(n string) resource.TestCheckFunc {
58-
return func(s *terraform.State) error {
59-
rs, ok := s.RootModule().Resources[n]
60+
return func(state *terraform.State) error {
61+
rs, ok := state.RootModule().Resources[n]
6062
if !ok {
6163
return fmt.Errorf("resource not found: %s", n)
6264
}

scaleway/data_source_registry_namespace_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
)
88

99
func TestAccScalewayDataSourceRegistryNamespace_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
1013
resource.ParallelTest(t, resource.TestCase{
11-
PreCheck: func() { testAccPreCheck(t) },
12-
Providers: testAccProviders,
13-
CheckDestroy: testAccCheckScalewayRegistryNamespaceBetaDestroy,
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProviderFactories: tt.ProviderFactories,
16+
CheckDestroy: testAccCheckScalewayRegistryNamespaceBetaDestroy(tt),
1417
Steps: []resource.TestStep{
1518
{
1619
Config: `
@@ -27,7 +30,7 @@ func TestAccScalewayDataSourceRegistryNamespace_Basic(t *testing.T) {
2730
}
2831
`,
2932
Check: resource.ComposeTestCheckFunc(
30-
testAccCheckScalewayRegistryNamespaceExists("scaleway_registry_namespace.test"),
33+
testAccCheckScalewayRegistryNamespaceExists(tt, "scaleway_registry_namespace.test"),
3134

3235
resource.TestCheckResourceAttr("scaleway_registry_namespace.test", "name", "test-cr"),
3336
resource.TestCheckResourceAttrSet("data.scaleway_registry_namespace.test", "id"),
@@ -58,7 +61,7 @@ func TestAccScalewayDataSourceRegistryNamespace_Basic(t *testing.T) {
5861
}
5962
`,
6063
Check: resource.ComposeTestCheckFunc(
61-
testAccCheckScalewayRegistryNamespaceExists("scaleway_registry_namespace.test"),
64+
testAccCheckScalewayRegistryNamespaceExists(tt, "scaleway_registry_namespace.test"),
6265

6366
resource.TestCheckResourceAttr("scaleway_registry_namespace.test", "name", "test-cr"),
6467
resource.TestCheckResourceAttrSet("data.scaleway_registry_namespace.test", "id"),

scaleway/resource_registry_namespace.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ func resourceScalewayRegistryNamespaceCreate(ctx context.Context, d *schema.Reso
5858
}
5959

6060
ns, err := api.CreateNamespace(&registry.CreateNamespaceRequest{
61-
Region: region,
62-
// Registry does not support yet project id.
63-
// Once it does, use ProjectID instead or OrganizationID
64-
OrganizationID: expandStringPtr(d.Get("project_id")),
65-
Name: d.Get("name").(string),
66-
Description: d.Get("description").(string),
67-
IsPublic: d.Get("is_public").(bool),
61+
Region: region,
62+
ProjectID: expandStringPtr(d.Get("project_id")),
63+
Name: d.Get("name").(string),
64+
Description: d.Get("description").(string),
65+
IsPublic: d.Get("is_public").(bool),
6866
}, scw.WithContext(ctx))
6967
if err != nil {
7068
return diag.FromErr(err)
@@ -96,9 +94,8 @@ func resourceScalewayRegistryNamespaceRead(ctx context.Context, d *schema.Resour
9694

9795
_ = d.Set("name", ns.Name)
9896
_ = d.Set("description", ns.Description)
99-
// Registry does not support yet project id.
100-
// Once it does, use ProjectID instead or OrganizationID
101-
_ = d.Set("project_id", ns.OrganizationID)
97+
_ = d.Set("organization_id", ns.OrganizationID)
98+
_ = d.Set("project_id", ns.ProjectID)
10299
_ = d.Set("is_public", ns.IsPublic)
103100
_ = d.Set("endpoint", ns.Endpoint)
104101
_ = d.Set("region", ns.Region)

scaleway/resource_registry_namespace_test.go

+34-29
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ func testSweepRegistryNamespace(_ string) error {
4040
}
4141

4242
func TestAccScalewayRegistryNamespace_Basic(t *testing.T) {
43+
tt := NewTestTools(t)
44+
defer tt.Cleanup()
45+
4346
resource.ParallelTest(t, resource.TestCase{
44-
PreCheck: func() { testAccPreCheck(t) },
45-
Providers: testAccProviders,
46-
CheckDestroy: testAccCheckScalewayRegistryNamespaceBetaDestroy,
47+
PreCheck: func() { testAccPreCheck(t) },
48+
ProviderFactories: tt.ProviderFactories,
49+
CheckDestroy: testAccCheckScalewayRegistryNamespaceBetaDestroy(tt),
4750
Steps: []resource.TestStep{
4851
{
4952
Config: `
@@ -52,7 +55,7 @@ func TestAccScalewayRegistryNamespace_Basic(t *testing.T) {
5255
}
5356
`,
5457
Check: resource.ComposeTestCheckFunc(
55-
testAccCheckScalewayRegistryNamespaceExists("scaleway_registry_namespace.cr01"),
58+
testAccCheckScalewayRegistryNamespaceExists(tt, "scaleway_registry_namespace.cr01"),
5659
resource.TestCheckResourceAttr("scaleway_registry_namespace.cr01", "name", "test-cr"),
5760
testCheckResourceAttrUUID("scaleway_registry_namespace.cr01", "id"),
5861
),
@@ -66,7 +69,7 @@ func TestAccScalewayRegistryNamespace_Basic(t *testing.T) {
6669
}
6770
`,
6871
Check: resource.ComposeTestCheckFunc(
69-
testAccCheckScalewayRegistryNamespaceExists("scaleway_registry_namespace.cr01"),
72+
testAccCheckScalewayRegistryNamespaceExists(tt, "scaleway_registry_namespace.cr01"),
7073
resource.TestCheckResourceAttr("scaleway_registry_namespace.cr01", "description", "test registry namespace"),
7174
resource.TestCheckResourceAttr("scaleway_registry_namespace.cr01", "is_public", "true"),
7275
testCheckResourceAttrUUID("scaleway_registry_namespace.cr01", "id"),
@@ -76,14 +79,14 @@ func TestAccScalewayRegistryNamespace_Basic(t *testing.T) {
7679
})
7780
}
7881

79-
func testAccCheckScalewayRegistryNamespaceExists(n string) resource.TestCheckFunc {
80-
return func(s *terraform.State) error {
81-
rs, ok := s.RootModule().Resources[n]
82+
func testAccCheckScalewayRegistryNamespaceExists(tt *TestTools, n string) resource.TestCheckFunc {
83+
return func(state *terraform.State) error {
84+
rs, ok := state.RootModule().Resources[n]
8285
if !ok {
8386
return fmt.Errorf("resource not found: %s", n)
8487
}
8588

86-
api, region, id, err := registryAPIWithRegionAndID(testAccProvider.Meta(), rs.Primary.ID)
89+
api, region, id, err := registryAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
8790
if err != nil {
8891
return nil
8992
}
@@ -101,30 +104,32 @@ func testAccCheckScalewayRegistryNamespaceExists(n string) resource.TestCheckFun
101104
}
102105
}
103106

104-
func testAccCheckScalewayRegistryNamespaceBetaDestroy(s *terraform.State) error {
105-
for _, rs := range s.RootModule().Resources {
106-
if rs.Type != "scaleway_registry_namespace" {
107-
continue
108-
}
107+
func testAccCheckScalewayRegistryNamespaceBetaDestroy(tt *TestTools) resource.TestCheckFunc {
108+
return func(state *terraform.State) error {
109+
for _, rs := range state.RootModule().Resources {
110+
if rs.Type != "scaleway_registry_namespace" {
111+
continue
112+
}
109113

110-
api, region, id, err := registryAPIWithRegionAndID(testAccProvider.Meta(), rs.Primary.ID)
111-
if err != nil {
112-
return err
113-
}
114+
api, region, id, err := registryAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
115+
if err != nil {
116+
return err
117+
}
114118

115-
_, err = api.DeleteNamespace(&registry.DeleteNamespaceRequest{
116-
NamespaceID: id,
117-
Region: region,
118-
})
119+
_, err = api.DeleteNamespace(&registry.DeleteNamespaceRequest{
120+
NamespaceID: id,
121+
Region: region,
122+
})
119123

120-
if err == nil {
121-
return fmt.Errorf("namespace (%s) still exists", rs.Primary.ID)
122-
}
124+
if err == nil {
125+
return fmt.Errorf("namespace (%s) still exists", rs.Primary.ID)
126+
}
123127

124-
if !is404Error(err) {
125-
return err
128+
if !is404Error(err) {
129+
return err
130+
}
126131
}
127-
}
128132

129-
return nil
133+
return nil
134+
}
130135
}

0 commit comments

Comments
 (0)