forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(container): add resource container namespace (scaleway#1107)
- Loading branch information
Showing
12 changed files
with
2,897 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ jobs: | |
- Account | ||
- AppleSilicon | ||
- Baremetal | ||
- Container | ||
- Domain | ||
- Function | ||
- Instance | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ jobs: | |
- Baremetal | ||
- Domain | ||
- Function | ||
- Container | ||
- Instance | ||
- Iot | ||
- K8S | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.