Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(function): add support for namespace #1104

Merged
merged 11 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/data-sources/function_namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
page_title: "Scaleway: scaleway_function_namespace"
description: |-
Gets information about a function namespace.
---

# scaleway_function_namespace

Gets information about a function namespace.

## Example Usage

```hcl
// Get info by namespace name
data "scaleway_function_namespace" "my_namespace" {
name = "my-namespace-name"
}

// Get info by namespace ID
data "scaleway_function_namespace" "my_namespace" {
namespace_id = "11111111-1111-1111-1111-111111111111"
}
```

## Argument Reference

- `name` - (Optional) The namespace name.
Only one of `name` and `namespace_id` should be specified.

- `namespace_id` - (Optional) The namespace id.
Only one of `name` and `namespace_id` should be specified.
-
remyleone marked this conversation as resolved.
Show resolved Hide resolved
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the namespace exists.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.

## Attributes Reference

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

- `id` - The ID of the Registry Namespace.
- `organization_id` - The organization ID the namespace is associated with.
- `description` - The description of the namespace.
- `environment_variables` - The environment variables of the namespace.
- `registry_endpoint` - The registry endpoint of the namespace.
- `registry_namespace_id` - The registry namespace ID of the namespace.
55 changes: 55 additions & 0 deletions docs/resources/function_namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
page_title: "Scaleway: scaleway_function_namespace"
description: |-
Manages Scaleway Function Namespaces.
---

# scaleway_function_namespace

Creates and manages Scaleway Function Namespace.
For more information see [the documentation](https://developers.scaleway.com/en/products/functions/api/).

## Examples

### Basic

```hcl
resource "scaleway_function_namespace" "main" {
name = "main-function-namespace"
description = "Main function namespace"
}
```

## Arguments Reference

The following arguments are supported:

- `name` - (Required) The unique name of the function namespace.

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

- `description` (Optional) The description of the namespace.

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

- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.

- `environment_variables` - The environment variables of the namespace.

## Attributes Reference

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

- `id` - The ID of the namespace
- `organization_id` - The organization ID the namespace is associated with.
- `registry_endpoint` - The registry endpoint of the namespace.
- `registry_namespace_id` - The registry namespace ID of the namespace.


## Import

Namespaces can be imported using the `{region}/{id}`, e.g.

```bash
$ terraform import scaleway_function_namespace.main fr-par/11111111-1111-1111-1111-111111111111
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20220112145133-71a550556c71
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572
github.com/stretchr/testify v1.7.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20220112145133-71a550556c71 h1:FzB5WvhIkSEaZHTy98WbLc0P4lK2b29bVIxI4/mer3A=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20220112145133-71a550556c71/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572 h1:/HLfjGzJDzgfUCO6lFf8rQIBUbPEDclXuyjtRw778vQ=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down
63 changes: 63 additions & 0 deletions scaleway/data_source_function_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package scaleway

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayFunctionNamespace() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayFunctionNamespace().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "region")

dsSchema["name"].ConflictsWith = []string{"namespace_id"}
dsSchema["namespace_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The ID of the function namespace",
ValidateFunc: validationUUIDorUUIDWithLocality(),
ConflictsWith: []string{"name"},
}

return &schema.Resource{
ReadContext: dataSourceScalewayFunctionNamespaceRead,
Schema: dsSchema,
}
}

func dataSourceScalewayFunctionNamespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, err := functionAPIWithRegion(d, meta)
if err != nil {
return diag.FromErr(err)
}

namespaceID, ok := d.GetOk("namespace_id")
if !ok {
res, err := api.ListNamespaces(&function.ListNamespacesRequest{
Region: region,
Name: expandStringPtr(d.Get("name")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
if len(res.Namespaces) == 0 {
return diag.FromErr(fmt.Errorf("no function namespaces found with the name %s", d.Get("name")))
}
if len(res.Namespaces) > 1 {
return diag.FromErr(fmt.Errorf("%d function namespaces found with the same name %s", len(res.Namespaces), d.Get("name")))
}
namespaceID = res.Namespaces[0].ID
}

regionalID := datasourceNewRegionalizedID(namespaceID, region)
d.SetId(regionalID)
_ = d.Set("namespace_id", regionalID)

return resourceScalewayFunctionNamespaceRead(ctx, d, meta)
}
43 changes: 43 additions & 0 deletions scaleway/data_source_function_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scaleway

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccScalewayDataSourceFunctionNamespace_Basic(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayFunctionNamespaceDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_function_namespace" "main" {
name = "test-cr-data"
}

data "scaleway_function_namespace" "by_name" {
name = scaleway_function_namespace.main.name
}

data "scaleway_function_namespace" "by_id" {
namespace_id = scaleway_function_namespace.main.id
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayFunctionNamespaceExists(tt, "scaleway_function_namespace.main"),
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "name", "test-cr-data"),
resource.TestCheckResourceAttrSet("data.scaleway_function_namespace.by_name", "id"),

resource.TestCheckResourceAttr("data.scaleway_function_namespace.by_id", "name", "test-cr-data"),
resource.TestCheckResourceAttrSet("data.scaleway_function_namespace.by_id", "id"),
),
},
},
})
}
11 changes: 11 additions & 0 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,14 @@ func TimedOut(err error) bool {
timeoutErr, ok := err.(*resource.TimeoutError) //nolint:errorlint // Explicitly does *not* match wrapped TimeoutErrors
return ok && timeoutErr.LastError == nil
}

func expandMapStringStringPtr(data interface{}) *map[string]string {
if data == nil {
return nil
}
m := make(map[string]string)
for k, v := range data.(map[string]interface{}) {
m[k] = v.(string)
}
return &m
}
37 changes: 37 additions & 0 deletions scaleway/helpers_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package scaleway

import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
defaultFunctionNamespaceTimeout = 5 * time.Minute
)

// functionAPIWithRegion returns a new container registry API and the region.
func functionAPIWithRegion(d *schema.ResourceData, m interface{}) (*function.API, scw.Region, error) {
meta := m.(*Meta)
api := function.NewAPI(meta.scwClient)

region, err := extractRegion(d, meta)
if err != nil {
return nil, "", err
}
return api, region, nil
}

// functionAPIWithRegionAndID returns a new container registry API, region and ID.
func functionAPIWithRegionAndID(m interface{}, id string) (*function.API, scw.Region, string, error) {
meta := m.(*Meta)
api := function.NewAPI(meta.scwClient)

region, id, err := parseRegionalID(id)
if err != nil {
return nil, "", "", err
}
return api, region, id, nil
}
2 changes: 2 additions & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
"scaleway_baremetal_server": resourceScalewayBaremetalServer(),
"scaleway_domain_record": resourceScalewayDomainRecord(),
"scaleway_domain_zone": resourceScalewayDomainZone(),
"scaleway_function_namespace": resourceScalewayFunctionNamespace(),
"scaleway_instance_ip": resourceScalewayInstanceIP(),
"scaleway_instance_ip_reverse_dns": resourceScalewayInstanceIPReverseDNS(),
"scaleway_instance_volume": resourceScalewayInstanceVolume(),
Expand Down Expand Up @@ -106,6 +107,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
"scaleway_baremetal_offer": dataSourceScalewayBaremetalOffer(),
"scaleway_domain_record": dataSourceScalewayDomainRecord(),
"scaleway_domain_zone": dataSourceScalewayDomainZone(),
"scaleway_function_namespace": dataSourceScalewayFunctionNamespace(),
"scaleway_instance_ip": dataSourceScalewayInstanceIP(),
"scaleway_instance_security_group": dataSourceScalewayInstanceSecurityGroup(),
"scaleway_instance_server": dataSourceScalewayInstanceServer(),
Expand Down