Skip to content

Commit ce7db06

Browse files
authored
Merge pull request #184 from yanndegat/containerregistry
cloud/project/containerregistry: resources + datasources
2 parents 0e8968d + 1968784 commit ce7db06

25 files changed

+2243
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/url"
7+
"sort"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
11+
)
12+
13+
func dataSourceCloudProjectCapabilitiesContainerRegistry() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceCloudProjectCapabilitiesContainerRegistryRead,
16+
Schema: map[string]*schema.Schema{
17+
"service_name": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
21+
},
22+
"result": {
23+
Type: schema.TypeList,
24+
Computed: true,
25+
Description: "List of container registry capability for a single region",
26+
Elem: &schema.Resource{
27+
Schema: map[string]*schema.Schema{
28+
"region_name": {
29+
Type: schema.TypeString,
30+
Description: "The region name",
31+
Computed: true,
32+
},
33+
"plans": {
34+
Type: schema.TypeList,
35+
Computed: true,
36+
Description: "Available plans in the region",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"code": {
40+
Type: schema.TypeString,
41+
Description: "Plan code from catalog",
42+
Computed: true,
43+
},
44+
"created_at": {
45+
Type: schema.TypeString,
46+
Description: "Plan creation date",
47+
Computed: true,
48+
},
49+
"features": {
50+
Type: schema.TypeList,
51+
Computed: true,
52+
Description: "Features of the plan",
53+
Elem: &schema.Resource{
54+
Schema: map[string]*schema.Schema{
55+
"vulnerability": {
56+
Type: schema.TypeBool,
57+
Description: "Vulnerability scanning",
58+
Computed: true,
59+
},
60+
},
61+
},
62+
},
63+
"id": {
64+
Type: schema.TypeString,
65+
Description: "Plan ID",
66+
Computed: true,
67+
},
68+
"name": {
69+
Type: schema.TypeString,
70+
Description: "Plan name",
71+
Computed: true,
72+
},
73+
"registry_limits": {
74+
Type: schema.TypeList,
75+
Computed: true,
76+
Description: "Container registry limits",
77+
Elem: &schema.Resource{
78+
Schema: map[string]*schema.Schema{
79+
"image_storage": {
80+
Type: schema.TypeInt,
81+
Description: "Docker image storage limits in bytes",
82+
83+
Computed: true,
84+
},
85+
"parallel_request": {
86+
Type: schema.TypeInt,
87+
Description: "Parallel requests on Docker image API (/v2 Docker registry API)",
88+
Computed: true,
89+
},
90+
},
91+
},
92+
},
93+
"updated_at": {
94+
Type: schema.TypeString,
95+
Description: "Plan last update date",
96+
Computed: true,
97+
},
98+
},
99+
},
100+
},
101+
},
102+
},
103+
},
104+
},
105+
}
106+
}
107+
108+
func dataSourceCloudProjectCapabilitiesContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
109+
config := meta.(*Config)
110+
serviceName := d.Get("service_name").(string)
111+
112+
log.Printf("[DEBUG] Will read cloud project capabilities container registry for project: %s", serviceName)
113+
114+
capregs := []CloudProjectCapabilitiesContainerRegistry{}
115+
116+
endpoint := fmt.Sprintf(
117+
"/cloud/project/%s/capabilities/containerRegistry",
118+
url.PathEscape(serviceName),
119+
)
120+
err := config.OVHClient.Get(endpoint, &capregs)
121+
if err != nil {
122+
return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
123+
}
124+
125+
mapcapregs := make([]map[string]interface{}, len(capregs))
126+
ids := make([]string, len(capregs))
127+
128+
for i, capreg := range capregs {
129+
mapcapregs[i] = capreg.ToMap()
130+
for _, plan := range capreg.Plans {
131+
ids = append(ids, plan.Id)
132+
}
133+
}
134+
135+
// sort.Strings sorts in place, returns nothing
136+
sort.Strings(ids)
137+
138+
d.SetId(hashcode.Strings(ids))
139+
d.Set("result", mapcapregs)
140+
141+
return nil
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceCloudProjectCapabilitiesContainerRegistryFilter() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceCloudProjectCapabilitiesContainerRegistryFilterRead,
14+
Schema: map[string]*schema.Schema{
15+
"service_name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
19+
},
20+
"region": {
21+
Type: schema.TypeString,
22+
Description: "Region of the registry.",
23+
Required: true,
24+
},
25+
"plan_name": {
26+
Type: schema.TypeString,
27+
Description: "Plan name of the registry.",
28+
Required: true,
29+
},
30+
31+
//Computed
32+
"code": {
33+
Type: schema.TypeString,
34+
Description: "Plan code from catalog",
35+
Computed: true,
36+
},
37+
"created_at": {
38+
Type: schema.TypeString,
39+
Description: "Plan creation date",
40+
Computed: true,
41+
},
42+
"features": {
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Description: "Features of the plan",
46+
Elem: &schema.Resource{
47+
Schema: map[string]*schema.Schema{
48+
"vulnerability": {
49+
Type: schema.TypeBool,
50+
Description: "Vulnerability scanning",
51+
Computed: true,
52+
},
53+
},
54+
},
55+
},
56+
"name": {
57+
Type: schema.TypeString,
58+
Description: "Plan name",
59+
Computed: true,
60+
},
61+
"registry_limits": {
62+
Type: schema.TypeList,
63+
Computed: true,
64+
Description: "Container registry limits",
65+
Elem: &schema.Resource{
66+
Schema: map[string]*schema.Schema{
67+
"image_storage": {
68+
Type: schema.TypeInt,
69+
Description: "Docker image storage limits in bytes",
70+
71+
Computed: true,
72+
},
73+
"parallel_request": {
74+
Type: schema.TypeInt,
75+
Description: "Parallel requests on Docker image API (/v2 Docker registry API)",
76+
Computed: true,
77+
},
78+
},
79+
},
80+
},
81+
"updated_at": {
82+
Type: schema.TypeString,
83+
Description: "Plan last update date",
84+
Computed: true,
85+
},
86+
},
87+
}
88+
}
89+
90+
func dataSourceCloudProjectCapabilitiesContainerRegistryFilterRead(d *schema.ResourceData, meta interface{}) error {
91+
config := meta.(*Config)
92+
serviceName := d.Get("service_name").(string)
93+
94+
log.Printf("[DEBUG] Will read cloud project capabilities container registry for project: %s", serviceName)
95+
96+
capregs := []CloudProjectCapabilitiesContainerRegistry{}
97+
98+
endpoint := fmt.Sprintf(
99+
"/cloud/project/%s/capabilities/containerRegistry",
100+
url.PathEscape(serviceName),
101+
)
102+
err := config.OVHClient.Get(endpoint, &capregs)
103+
if err != nil {
104+
return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
105+
}
106+
107+
match := false
108+
for _, capreg := range capregs {
109+
if capreg.RegionName == d.Get("region").(string) {
110+
for _, plan := range capreg.Plans {
111+
if plan.Name == d.Get("plan_name").(string) {
112+
for k, v := range plan.ToMap() {
113+
match = true
114+
if k == "id" {
115+
d.SetId(v.(string))
116+
} else {
117+
d.Set(k, v)
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
124+
125+
if !match {
126+
return fmt.Errorf("Your query returned no results. " +
127+
"Please change your search criteria and try again.")
128+
}
129+
130+
return nil
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccCloudProjectCapabilitiesContainerRegistryFilterDataSource_basic(t *testing.T) {
12+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
13+
planName := "SMALL"
14+
region := "GRA"
15+
16+
config := fmt.Sprintf(
17+
testAccCloudProjectCapabilitiesContainerRegistryFilterDatasourceConfig_Basic,
18+
serviceName,
19+
region,
20+
planName,
21+
)
22+
23+
resource.Test(t, resource.TestCase{
24+
PreCheck: func() { testAccPreCheckCloud(t) },
25+
Providers: testAccProviders,
26+
Steps: []resource.TestStep{
27+
{
28+
Config: config,
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttr(
31+
"data.ovh_cloud_project_capabilities_containerregistry_filter.cap",
32+
"region",
33+
"GRA",
34+
),
35+
resource.TestCheckResourceAttr(
36+
"data.ovh_cloud_project_capabilities_containerregistry_filter.cap",
37+
"name",
38+
planName,
39+
),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
const testAccCloudProjectCapabilitiesContainerRegistryFilterDatasourceConfig_Basic = `
47+
data "ovh_cloud_project_capabilities_containerregistry_filter" "cap" {
48+
service_name = "%s"
49+
region = "%s"
50+
plan_name = "%s"
51+
}
52+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccCloudProjectCapabilitiesContainerRegistryDataSource_basic(t *testing.T) {
12+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
13+
config := fmt.Sprintf(
14+
testAccCloudProjectCapabilitiesContainerRegistryDatasourceConfig_Basic,
15+
serviceName,
16+
)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheckCloud(t) },
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: config,
24+
Check: resource.ComposeTestCheckFunc(
25+
resource.TestCheckResourceAttrSet(
26+
"data.ovh_cloud_project_capabilities_containerregistry.cap",
27+
"result.#",
28+
),
29+
resource.TestCheckResourceAttr(
30+
"data.ovh_cloud_project_capabilities_containerregistry.cap",
31+
"result.0.region_name", "GRA"),
32+
resource.TestCheckResourceAttrSet(
33+
"data.ovh_cloud_project_capabilities_containerregistry.cap",
34+
"result.0.plans.#",
35+
),
36+
resource.TestCheckResourceAttrSet(
37+
"data.ovh_cloud_project_capabilities_containerregistry.cap",
38+
"result.0.plans.0.code",
39+
),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
const testAccCloudProjectCapabilitiesContainerRegistryDatasourceConfig_Basic = `
47+
data "ovh_cloud_project_capabilities_containerregistry" "cap" {
48+
service_name = "%s"
49+
}
50+
`

0 commit comments

Comments
 (0)