Skip to content

Commit edfbe4e

Browse files
author
Erwan Morvan
committed
feat(container-registry): add OVHcloud IAM feature for Managed Registry product
Terraform is able to enable/disable OVHcloud IAM feature for Managed Registry product
1 parent 416c59a commit edfbe4e

13 files changed

+499
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory : "Managed Private Registry (MPR)"
3+
---
4+
5+
# ovh_cloud_project_containerregistry_iam (Data Source)
6+
7+
Use this data source to get a OVHcloud Managed Private Registry through OVHcloud IAM.
8+
9+
## Example Usage
10+
11+
```hcl
12+
data "ovh_cloud_project_containerregistry_iam" "my_iam" {
13+
service_name = "XXXXXX"
14+
registry_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
15+
}
16+
17+
output "iam-enabled" {
18+
value = data.ovh_cloud_project_containerregistry_iam.my_iam.iam_enabled
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `service_name` - (Optional) The id of the public cloud project. If omitted, the `OVH_CLOUD_PROJECT_SERVICE` environment variable is used.
27+
* `registry_id` - The id of the Managed Private Registry.
28+
29+
## Attributes Reference
30+
31+
The following attributes are exported:
32+
33+
* `service_name` - The ID of the public cloud project. If omitted, the `OVH_CLOUD_PROJECT_SERVICE` environment variable is used.
34+
* `registry_id` - The ID of the Managed Private Registry.
35+
* `iam-enabled` - OVHcloud IAM feature status.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
subcategory : "Managed Private Registry (MPR)"
3+
---
4+
5+
# ovh_cloud_project_containerregistry_iam
6+
7+
Creates an OVHcloud IAM configuration in an OVHcloud Managed Private Registry.
8+
9+
## Example Usage
10+
11+
```hcl
12+
resource "ovh_cloud_project_containerregistry_iam" "my_iam" {
13+
service_name = "XXXXXX"
14+
registry_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
15+
16+
#optional field
17+
delete_users = false
18+
}
19+
20+
output "iam-enabled" {
21+
value = ovh_cloud_project_containerregistry_iam.my_iam.iam-enabled
22+
sensitive = true
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
The following arguments are supported:
29+
30+
* `service_name` - The ID of the public cloud project. If omitted, the `OVH_CLOUD_PROJECT_SERVICE` environment variable is used. **Changing this value recreates the resource.**
31+
* `registry_id` - The ID of the Managed Private Registry. **Changing this value recreates the resource.**
32+
* `delete_users` - Delete existing users from Harbor. IAM feature can't be enabled if there is at least one user already created. This parameter is only used at IAM configuration creation. **Changing this value recreates the resource.**
33+
34+
## Timeouts
35+
36+
```hcl
37+
resource "ovh_cloud_project_containerregistry_iam" "my-iam" {
38+
# ...
39+
40+
timeouts {
41+
create = "1h"
42+
update = "45m"
43+
delete = "50s"
44+
}
45+
}
46+
```
47+
48+
* `create` - (Default 10m)
49+
* `update` - (Default 10m)
50+
* `delete` - (Default 10m)
51+
52+
## Import
53+
54+
OVHcloud Managed Private Registry OVHcloud IAM can be imported using the tenant `service_name` and registry id `registry_id` separated by "/" E.g.,
55+
56+
```bash
57+
$ terraform import ovh_cloud_project_containerregistry_iam.my-iam service_name/registry_id
58+
```

ovh/data_cloud_project_containerregistries.go

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func dataSourceCloudProjectContainerRegistries() *schema.Resource {
2929
Description: "Registry creation date",
3030
Computed: true,
3131
},
32+
"iam_enabled": {
33+
Type: schema.TypeBool,
34+
Description: "OVHcloud IAM enabled",
35+
Computed: true,
36+
},
3237
"id": {
3338
Type: schema.TypeString,
3439
Description: "Registry ID",

ovh/data_cloud_project_containerregistry.go

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func dataSourceCloudProjectContainerRegistry() *schema.Resource {
2929
Description: "Registry creation date",
3030
Computed: true,
3131
},
32+
"iam_enabled": {
33+
Type: schema.TypeBool,
34+
Description: "OVHcloud IAM enabled",
35+
Computed: true,
36+
},
3237
"name": {
3338
Type: schema.TypeString,
3439
Description: "Registry name",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceCloudProjectContainerRegistryIAM() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceCloudProjectContainerRegistryIAMRead,
13+
Schema: map[string]*schema.Schema{
14+
"service_name": {
15+
Type: schema.TypeString,
16+
Description: "Service name",
17+
Required: true,
18+
ForceNew: true,
19+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
20+
},
21+
"registry_id": {
22+
Type: schema.TypeString,
23+
Description: "Registry ID",
24+
Required: true,
25+
ForceNew: true,
26+
},
27+
"iam_enabled": {
28+
Type: schema.TypeBool,
29+
Description: "OVHcloud IAM enabled",
30+
Computed: true,
31+
},
32+
},
33+
}
34+
}
35+
36+
func dataSourceCloudProjectContainerRegistryIAMRead(d *schema.ResourceData, meta any) error {
37+
config := meta.(*Config)
38+
serviceName := d.Get("service_name").(string)
39+
registryID := d.Get("registry_id").(string)
40+
41+
endpoint := fmt.Sprintf("/cloud/project/%s/containerRegistry/%s", serviceName, registryID)
42+
res := &CloudProjectContainerRegistry{}
43+
44+
log.Printf("[DEBUG] Will read from registry %s and project: %s", registryID, serviceName)
45+
46+
err := config.OVHClient.Get(endpoint, res)
47+
if err != nil {
48+
return fmt.Errorf("calling get %s %w", endpoint, err)
49+
}
50+
51+
for k, v := range res.ToMap() {
52+
if k == "iam_enabled" {
53+
d.Set(k, v)
54+
}
55+
}
56+
57+
d.SetId(serviceName + "/" + registryID)
58+
59+
log.Printf("[DEBUG] Read IAM %+v", res)
60+
61+
return nil
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
)
11+
12+
func TestAccCloudProjectContainerRegistryIAMDataSource_basic(t *testing.T) {
13+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
14+
region := os.Getenv("OVH_CLOUD_PROJECT_CONTAINERREGISTRY_REGION_TEST")
15+
registryName := acctest.RandomWithPrefix(test_prefix)
16+
17+
config := fmt.Sprintf(
18+
testAccCloudProjectContainerRegistryIAMDataSourceConfig,
19+
serviceName,
20+
region,
21+
registryName,
22+
)
23+
24+
resource.Test(t, resource.TestCase{
25+
PreCheck: func() {
26+
testAccPreCheckContainerRegistryIAM(t)
27+
},
28+
Providers: testAccProviders,
29+
Steps: []resource.TestStep{
30+
{
31+
Config: config,
32+
Check: resource.ComposeTestCheckFunc(
33+
resource.TestCheckResourceAttr(
34+
"data.ovh_cloud_project_containerregistry_iam.iamData", "iam_enabled", "true"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
var testAccCloudProjectContainerRegistryIAMDataSourceConfig = `
42+
data "ovh_cloud_project_capabilities_containerregistry_filter" "registryCap" {
43+
service_name = "%s"
44+
plan_name = "SMALL"
45+
region = "%s"
46+
}
47+
48+
resource "ovh_cloud_project_containerregistry" "registry" {
49+
service_name = data.ovh_cloud_project_capabilities_containerregistry_filter.registryCap.service_name
50+
plan_id = data.ovh_cloud_project_capabilities_containerregistry_filter.registryCap.id
51+
name = "%s"
52+
region = data.ovh_cloud_project_capabilities_containerregistry_filter.registryCap.region
53+
}
54+
55+
resource "ovh_cloud_project_containerregistry_iam" "iam" {
56+
service_name = ovh_cloud_project_containerregistry.registry.service_name
57+
registry_id = ovh_cloud_project_containerregistry.registry.id
58+
59+
depends_on = [
60+
ovh_cloud_project_containerregistry.registry
61+
]
62+
}
63+
64+
data "ovh_cloud_project_containerregistry_iam" "iamData" {
65+
service_name = ovh_cloud_project_containerregistry.registry.service_name
66+
registry_id = ovh_cloud_project_containerregistry.registry.id
67+
68+
depends_on = [
69+
ovh_cloud_project_containerregistry_iam.iam
70+
]
71+
}
72+
`

ovh/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func Provider() *schema.Provider {
8282
"ovh_cloud_project_containerregistries": dataSourceCloudProjectContainerRegistries(),
8383
"ovh_cloud_project_containerregistry": dataSourceCloudProjectContainerRegistry(),
8484
"ovh_cloud_project_containerregistry_oidc": dataSourceCloudProjectContainerRegistryOIDC(),
85+
"ovh_cloud_project_containerregistry_iam": dataSourceCloudProjectContainerRegistryIAM(),
8586
"ovh_cloud_project_containerregistry_users": dataSourceCloudProjectContainerRegistryUsers(),
8687
"ovh_cloud_project_database": dataSourceCloudProjectDatabase(),
8788
"ovh_cloud_project_databases": dataSourceCloudProjectDatabases(),
@@ -188,6 +189,7 @@ func Provider() *schema.Provider {
188189
"ovh_cloud_project": resourceCloudProject(),
189190
"ovh_cloud_project_containerregistry": resourceCloudProjectContainerRegistry(),
190191
"ovh_cloud_project_containerregistry_oidc": resourceCloudProjectContainerRegistryOIDC(),
192+
"ovh_cloud_project_containerregistry_iam": resourceCloudProjectContainerRegistryIAM(),
191193
"ovh_cloud_project_containerregistry_user": resourceCloudProjectContainerRegistryUser(),
192194
"ovh_cloud_project_containerregistry_ip_restrictions_management": resourceCloudProjectContainerRegistryIPRestrictionsManagement(),
193195
"ovh_cloud_project_containerregistry_ip_restrictions_registry": resourceCloudProjectContainerRegistryIPRestrictionsRegistry(),

ovh/provider_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ func testAccPreCheckContainerRegistryOIDC(t *testing.T) {
267267
checkEnvOrSkip(t, "OVH_CLOUD_PROJECT_CONTAINERREGISTRY_OIDC_ENDPOINT_TEST")
268268
}
269269

270+
// Checks that the environment variables needed for the /cloud/{cloudId}/containerregistry/{registryID}/iam acceptance tests
271+
// are set.
272+
func testAccPreCheckContainerRegistryIAM(t *testing.T) {
273+
testAccPreCheckContainerRegistry(t)
274+
}
275+
270276
// Checks that the environment variables needed for the /cloud/project/{projectId}/network/private/ acceptance tests are set.
271277
func testAccPreCheckCloudNetworkPrivate(t *testing.T) {
272278
testAccPreCheckCloud(t)
@@ -485,7 +491,6 @@ func testAccCheckDomainZoneExists(t *testing.T) {
485491
}
486492

487493
t.Logf("Read Domain Zone %s -> nameservers: '%v'", endpoint, r.NameServers)
488-
489494
}
490495

491496
func testAccPreCheckDedicatedCeph(t *testing.T) {

ovh/resource_cloud_project_containerregistry.go

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func resourceCloudProjectContainerRegistry() *schema.Resource {
5454
Description: "Registry creation date",
5555
Computed: true,
5656
},
57+
"iam_enabled": {
58+
Type: schema.TypeBool,
59+
Description: "OVHcloud IAM enabled",
60+
Computed: true,
61+
},
5762
"project_id": {
5863
Type: schema.TypeString,
5964
Description: "Project ID of your registry",

0 commit comments

Comments
 (0)