forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support IBM-Cloud#2119 IBM-Cloud#1863: Container Registry Namespace
- Loading branch information
Showing
14 changed files
with
683 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Example for Container Registry resources | ||
|
||
This example illustrates how to use the Container Registry resources to create a namespace on to an account | ||
|
||
These types of resources are supported: | ||
|
||
* [ Namespace ](https://cloud.ibm.com/docs/Registry?topic=container-registry-cli-plugin-containerregcli#bx_cr_namespace_add) | ||
|
||
## Terraform versions | ||
|
||
Terraform 0.12. Pin module version to `~> v1.18.0`. Branch - `master`. | ||
|
||
## Usage | ||
|
||
To run this example you need to execute: | ||
|
||
```bash | ||
$ terraform init | ||
$ terraform plan | ||
$ terraform apply | ||
``` | ||
|
||
Run `terraform destroy` when you don't need these resources. | ||
|
||
|
||
## Container Registry Resources | ||
|
||
`Creating Namespace`: | ||
|
||
```hcl | ||
resource "ibm_cr_namespace" "namespace" { | ||
name = var.name | ||
resource_group_id = data.ibm_resource_group.rg.id | ||
} | ||
``` | ||
## Container Registry Data Source | ||
`List all Namespaces:` | ||
|
||
```hcl | ||
data "ibm_cr_namespaces" "namespaces"{} | ||
``` | ||
|
||
## Examples | ||
|
||
* [ Container Registry ](./main.tf) | ||
|
||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Requirements | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| terraform | ~> 0.12 | | ||
|
||
## Providers | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| ibm | n/a | | ||
|
||
## Inputs | ||
|
||
| Name | Description | Type | Required | Default | | ||
|------|-------------|------|---------| | ||
| name | Name Space Name. Force New Attribute| `string` | yes | N/A | | ||
| resource_group_id | The Id of resource group to which the namespace has to be imported. Force New attribute | `string` | No | Default RG| | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| namespace_crn | CRN of namespace resource. | | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
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,10 @@ | ||
provider "ibm" { | ||
region = "eu-gb" | ||
} | ||
data "ibm_resource_group" "rg" { | ||
name = var.resource_group_name | ||
} | ||
resource "ibm_cr_namespace" "namespace" { | ||
name = var.name | ||
resource_group_id = data.ibm_resource_group.rg.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,3 @@ | ||
output "namespace_crn" { | ||
value = ibm_cr_namespace.namespace.crn | ||
} |
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,8 @@ | ||
variable "name" { | ||
type= string | ||
description = "Name of the Namespace that has to be created" | ||
} | ||
variable "resource_group_name" { | ||
type= string | ||
description = "Name of the resource group in which Namespace that has to be created" | ||
} |
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
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
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,88 @@ | ||
package ibm | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
|
||
registryv1 "github.com/IBM-Cloud/bluemix-go/api/container/registryv1" | ||
) | ||
|
||
func dataIBMContainerRegistryNamespaces() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataIBMContainerRegistryNamespacesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"namespaces": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "Container Registry Namespaces", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Container Registry Namespace name", | ||
}, | ||
"resource_group_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Resource Group to which namespace has to be assigned", | ||
}, | ||
"crn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "CRN of the Namespace", | ||
}, | ||
"created_on": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Created Date", | ||
}, | ||
"updated_on": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Updated Date", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataIBMContainerRegistryNamespacesRead(d *schema.ResourceData, meta interface{}) error { | ||
userDetails, err := meta.(ClientSession).BluemixUserDetails() | ||
if err != nil { | ||
return err | ||
} | ||
accountID := userDetails.userAccount | ||
|
||
crClient, err := meta.(ClientSession).ContainerRegistryAPI() | ||
if err != nil { | ||
return err | ||
} | ||
target := registryv1.NamespaceTargetHeader{ | ||
AccountID: accountID, | ||
} | ||
|
||
crAPI := crClient.Namespaces() | ||
|
||
response, err := crAPI.GetDetailedNamespaces(target) | ||
if err != nil { | ||
return err | ||
} | ||
namespaces := []map[string]interface{}{} | ||
for _, ns := range response { | ||
namespace := map[string]interface{}{} | ||
namespace["name"] = ns.Name | ||
namespace["resource_group_id"] = ns.ResourceGroup | ||
namespace["crn"] = ns.CRN | ||
namespace["created_on"] = ns.CreatedDate | ||
namespace["updated_on"] = ns.UpdatedDate | ||
namespaces = append(namespaces, namespace) | ||
} | ||
d.Set("namespaces", namespaces) | ||
d.SetId(time.Now().UTC().String()) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccIBMCrNamespacesDataSourceBasic(t *testing.T) { | ||
namespaceName := fmt.Sprintf("terraform-tf-%d", acctest.RandIntRange(10, 100)) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckIBMCrNamespacesDataSourceConfig(namespaceName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.ibm_cr_namespaces.namespaces", "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMCrNamespacesDataSourceConfig(namespaceName string) string { | ||
return testAccCheckIBMCrNamespaceBasic(namespaceName) + fmt.Sprintf(` | ||
data "ibm_cr_namespaces" "namespaces" {} | ||
`) | ||
} |
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.