Skip to content

Commit 5b6860b

Browse files
committed
feat(function): add support for namespace (scaleway#1104)
1 parent 32a1c54 commit 5b6860b

14 files changed

+2065
-3
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
page_title: "Scaleway: scaleway_function_namespace"
3+
description: |-
4+
Gets information about a function namespace.
5+
---
6+
7+
# scaleway_function_namespace
8+
9+
Gets information about a function namespace.
10+
11+
## Example Usage
12+
13+
```hcl
14+
// Get info by namespace name
15+
data "scaleway_function_namespace" "my_namespace" {
16+
name = "my-namespace-name"
17+
}
18+
19+
// Get info by namespace ID
20+
data "scaleway_function_namespace" "my_namespace" {
21+
namespace_id = "11111111-1111-1111-1111-111111111111"
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
- `name` - (Optional) The namespace name.
28+
Only one of `name` and `namespace_id` should be specified.
29+
30+
- `namespace_id` - (Optional) The namespace id.
31+
Only one of `name` and `namespace_id` should be specified.
32+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the namespace exists.
33+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.
34+
35+
## Attributes Reference
36+
37+
In addition to all above arguments, the following attributes are exported:
38+
39+
- `id` - The ID of the Registry Namespace.
40+
- `organization_id` - The organization ID the namespace is associated with.
41+
- `description` - The description of the namespace.
42+
- `environment_variables` - The environment variables of the namespace.
43+
- `registry_endpoint` - The registry endpoint of the namespace.
44+
- `registry_namespace_id` - The registry namespace ID of the namespace.

docs/resources/function_namespace.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
page_title: "Scaleway: scaleway_function_namespace"
3+
description: |-
4+
Manages Scaleway Function Namespaces.
5+
---
6+
7+
# scaleway_function_namespace
8+
9+
Creates and manages Scaleway Function Namespace.
10+
For more information see [the documentation](https://developers.scaleway.com/en/products/functions/api/).
11+
12+
## Examples
13+
14+
### Basic
15+
16+
```hcl
17+
resource "scaleway_function_namespace" "main" {
18+
name = "main-function-namespace"
19+
description = "Main function namespace"
20+
}
21+
```
22+
23+
## Arguments Reference
24+
25+
The following arguments are supported:
26+
27+
- `name` - (Required) The unique name of the function namespace.
28+
29+
~> **Important** Updates to `name` will recreate the namespace.
30+
31+
- `description` (Optional) The description of the namespace.
32+
33+
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the namespace should be created.
34+
35+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.
36+
37+
- `environment_variables` - The environment variables of the namespace.
38+
39+
## Attributes Reference
40+
41+
In addition to all arguments above, the following attributes are exported:
42+
43+
- `id` - The ID of the namespace
44+
- `organization_id` - The organization ID the namespace is associated with.
45+
- `registry_endpoint` - The registry endpoint of the namespace.
46+
- `registry_namespace_id` - The registry namespace ID of the namespace.
47+
48+
49+
## Import
50+
51+
Namespaces can be imported using the `{region}/{id}`, e.g.
52+
53+
```bash
54+
$ terraform import scaleway_function_namespace.main fr-par/11111111-1111-1111-1111-111111111111
55+
```

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
1010
github.com/hashicorp/go-retryablehttp v0.7.0
1111
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
12-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20220112145133-71a550556c71
12+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572
1313
github.com/stretchr/testify v1.7.0
1414
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
1515

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
307307
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
308308
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
309309
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
310-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20220112145133-71a550556c71 h1:FzB5WvhIkSEaZHTy98WbLc0P4lK2b29bVIxI4/mer3A=
311-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20220112145133-71a550556c71/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
310+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572 h1:/HLfjGzJDzgfUCO6lFf8rQIBUbPEDclXuyjtRw778vQ=
311+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
312312
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
313313
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
314314
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func dataSourceScalewayFunctionNamespace() *schema.Resource {
14+
// Generate datasource schema from resource
15+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayFunctionNamespace().Schema)
16+
17+
addOptionalFieldsToSchema(dsSchema, "name", "region")
18+
19+
dsSchema["name"].ConflictsWith = []string{"namespace_id"}
20+
dsSchema["namespace_id"] = &schema.Schema{
21+
Type: schema.TypeString,
22+
Optional: true,
23+
Description: "The ID of the function namespace",
24+
ValidateFunc: validationUUIDorUUIDWithLocality(),
25+
ConflictsWith: []string{"name"},
26+
}
27+
28+
return &schema.Resource{
29+
ReadContext: dataSourceScalewayFunctionNamespaceRead,
30+
Schema: dsSchema,
31+
}
32+
}
33+
34+
func dataSourceScalewayFunctionNamespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
35+
api, region, err := functionAPIWithRegion(d, meta)
36+
if err != nil {
37+
return diag.FromErr(err)
38+
}
39+
40+
namespaceID, ok := d.GetOk("namespace_id")
41+
if !ok {
42+
res, err := api.ListNamespaces(&function.ListNamespacesRequest{
43+
Region: region,
44+
Name: expandStringPtr(d.Get("name")),
45+
}, scw.WithContext(ctx))
46+
if err != nil {
47+
return diag.FromErr(err)
48+
}
49+
if len(res.Namespaces) == 0 {
50+
return diag.FromErr(fmt.Errorf("no function namespaces found with the name %s", d.Get("name")))
51+
}
52+
if len(res.Namespaces) > 1 {
53+
return diag.FromErr(fmt.Errorf("%d function namespaces found with the same name %s", len(res.Namespaces), d.Get("name")))
54+
}
55+
namespaceID = res.Namespaces[0].ID
56+
}
57+
58+
regionalID := datasourceNewRegionalizedID(namespaceID, region)
59+
d.SetId(regionalID)
60+
_ = d.Set("namespace_id", regionalID)
61+
62+
return resourceScalewayFunctionNamespaceRead(ctx, d, meta)
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceFunctionNamespace_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
13+
resource.ParallelTest(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProviderFactories: tt.ProviderFactories,
16+
CheckDestroy: testAccCheckScalewayFunctionNamespaceDestroy(tt),
17+
Steps: []resource.TestStep{
18+
{
19+
Config: `
20+
resource "scaleway_function_namespace" "main" {
21+
name = "test-cr-data"
22+
}
23+
24+
data "scaleway_function_namespace" "by_name" {
25+
name = scaleway_function_namespace.main.name
26+
}
27+
28+
data "scaleway_function_namespace" "by_id" {
29+
namespace_id = scaleway_function_namespace.main.id
30+
}
31+
`,
32+
Check: resource.ComposeTestCheckFunc(
33+
testAccCheckScalewayFunctionNamespaceExists(tt, "scaleway_function_namespace.main"),
34+
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "name", "test-cr-data"),
35+
resource.TestCheckResourceAttrSet("data.scaleway_function_namespace.by_name", "id"),
36+
37+
resource.TestCheckResourceAttr("data.scaleway_function_namespace.by_id", "name", "test-cr-data"),
38+
resource.TestCheckResourceAttrSet("data.scaleway_function_namespace.by_id", "id"),
39+
),
40+
},
41+
},
42+
})
43+
}

scaleway/helpers.go

+11
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,14 @@ func TimedOut(err error) bool {
538538
timeoutErr, ok := err.(*resource.TimeoutError) //nolint:errorlint // Explicitly does *not* match wrapped TimeoutErrors
539539
return ok && timeoutErr.LastError == nil
540540
}
541+
542+
func expandMapStringStringPtr(data interface{}) *map[string]string {
543+
if data == nil {
544+
return nil
545+
}
546+
m := make(map[string]string)
547+
for k, v := range data.(map[string]interface{}) {
548+
m[k] = v.(string)
549+
}
550+
return &m
551+
}

scaleway/helpers_function.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package scaleway
2+
3+
import (
4+
"time"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1"
8+
"github.com/scaleway/scaleway-sdk-go/scw"
9+
)
10+
11+
const (
12+
defaultFunctionNamespaceTimeout = 5 * time.Minute
13+
)
14+
15+
// functionAPIWithRegion returns a new container registry API and the region.
16+
func functionAPIWithRegion(d *schema.ResourceData, m interface{}) (*function.API, scw.Region, error) {
17+
meta := m.(*Meta)
18+
api := function.NewAPI(meta.scwClient)
19+
20+
region, err := extractRegion(d, meta)
21+
if err != nil {
22+
return nil, "", err
23+
}
24+
return api, region, nil
25+
}
26+
27+
// functionAPIWithRegionAndID returns a new container registry API, region and ID.
28+
func functionAPIWithRegionAndID(m interface{}, id string) (*function.API, scw.Region, string, error) {
29+
meta := m.(*Meta)
30+
api := function.NewAPI(meta.scwClient)
31+
32+
region, id, err := parseRegionalID(id)
33+
if err != nil {
34+
return nil, "", "", err
35+
}
36+
return api, region, id, nil
37+
}

0 commit comments

Comments
 (0)