Skip to content

Commit

Permalink
feat(container): add resource container namespace (scaleway#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Monitob authored Feb 25, 2022
1 parent c0bcb9e commit ed56ada
Show file tree
Hide file tree
Showing 12 changed files with 2,897 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/acceptance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- Account
- AppleSilicon
- Baremetal
- Container
- Domain
- Function
- Instance
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- Baremetal
- Domain
- Function
- Container
- Instance
- Iot
- K8S
Expand Down
44 changes: 44 additions & 0 deletions docs/data-sources/container_namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
page_title: "Scaleway: scaleway_container_namespace"
description: |-
Gets information about a container namespace.
---

# scaleway_container_namespace

Gets information about a container namespace.

## Example Usage

```hcl
// Get info by namespace name
data "scaleway_container_namespace" "by_name" {
name = "my-namespace-name"
}
// Get info by namespace ID
data "scaleway_container_namespace" "by_id" {
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.
- `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/container_namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
page_title: "Scaleway: scaleway_container_namespace"
description: |-
Manages Scaleway Container Namespaces.
---

# scaleway_container_namespace

Creates and manages Scaleway Container Namespace.
For more information see [the documentation](https://developers.scaleway.com/en/products/containers/api/#namespaces-cdce79).

## Examples

### Basic

```hcl
resource "scaleway_container_namespace" "main" {
name = "main-container-namespace"
description = "Main container namespace"
}
```

## Arguments Reference

The following arguments are supported:

- `name` - (Required) The unique name of the container 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_container_namespace.main fr-par/11111111-1111-1111-1111-111111111111
```
63 changes: 63 additions & 0 deletions scaleway/data_source_container_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"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayContainerNamespace() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayContainerNamespace().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 Container namespace",
ValidateFunc: validationUUIDorUUIDWithLocality(),
ConflictsWith: []string{"name"},
}

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

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

namespaceID, ok := d.GetOk("namespace_id")
if !ok {
res, err := api.ListNamespaces(&container.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 container namespace found with the name %s", d.Get("name")))
}
if len(res.Namespaces) > 1 {
return diag.FromErr(fmt.Errorf("%d container 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 resourceScalewayContainerNamespaceRead(ctx, d, meta)
}
43 changes: 43 additions & 0 deletions scaleway/data_source_container_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 TestAccScalewayDataSourceContainerNamespace_Basic(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayContainerNamespaceDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_container_namespace" "main" {
name = "test-cr-data"
}
data "scaleway_container_namespace" "by_name" {
name = scaleway_container_namespace.main.name
}
data "scaleway_container_namespace" "by_id" {
namespace_id = scaleway_container_namespace.main.id
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayContainerNamespaceExists(tt, "scaleway_container_namespace.main"),
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "name", "test-cr-data"),
resource.TestCheckResourceAttrSet("data.scaleway_container_namespace.by_name", "id"),

resource.TestCheckResourceAttr("data.scaleway_container_namespace.by_id", "name", "test-cr-data"),
resource.TestCheckResourceAttrSet("data.scaleway_container_namespace.by_id", "id"),
),
},
},
})
}
37 changes: 37 additions & 0 deletions scaleway/helpers_container.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"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
defaultContainerNamespaceTimeout = 20 * time.Second
)

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

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

// containerAPIWithRegionAndID returns a new container API, region and ID.
func containerAPIWithRegionAndID(m interface{}, id string) (*container.API, scw.Region, string, error) {
meta := m.(*Meta)
api := container.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 @@ -63,6 +63,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
"scaleway_account_ssh_key": resourceScalewayAccountSSKKey(),
"scaleway_apple_silicon_server": resourceScalewayAppleSiliconServer(),
"scaleway_baremetal_server": resourceScalewayBaremetalServer(),
"scaleway_container_namespace": resourceScalewayContainerNamespace(),
"scaleway_domain_record": resourceScalewayDomainRecord(),
"scaleway_domain_zone": resourceScalewayDomainZone(),
"scaleway_function_namespace": resourceScalewayFunctionNamespace(),
Expand Down Expand Up @@ -107,6 +108,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
"scaleway_baremetal_offer": dataSourceScalewayBaremetalOffer(),
"scaleway_domain_record": dataSourceScalewayDomainRecord(),
"scaleway_domain_zone": dataSourceScalewayDomainZone(),
"scaleway_container_namespace": dataSourceScalewayContainerNamespace(),
"scaleway_function_namespace": dataSourceScalewayFunctionNamespace(),
"scaleway_instance_ip": dataSourceScalewayInstanceIP(),
"scaleway_instance_security_group": dataSourceScalewayInstanceSecurityGroup(),
Expand Down
Loading

0 comments on commit ed56ada

Please sign in to comment.