Skip to content

Commit

Permalink
Support IBM-Cloud#2119 IBM-Cloud#1863: Container Registry Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kavya498 committed Dec 23, 2020
1 parent db374eb commit 4d3eaf9
Show file tree
Hide file tree
Showing 14 changed files with 683 additions and 2 deletions.
76 changes: 76 additions & 0 deletions examples/ibm-container-registry/README.md
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 -->
10 changes: 10 additions & 0 deletions examples/ibm-container-registry/main.tf
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
}
3 changes: 3 additions & 0 deletions examples/ibm-container-registry/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "namespace_crn" {
value = ibm_cr_namespace.namespace.crn
}
8 changes: 8 additions & 0 deletions examples/ibm-container-registry/variables.tf
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"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/IBM-Cloud/bluemix-go v0.0.0-20201214130426-d34fed0e9aff
github.com/IBM-Cloud/bluemix-go v0.0.0-20201222104207-ac387d69450c
github.com/IBM-Cloud/power-go-client v1.0.53
github.com/IBM/apigateway-go-sdk v0.0.0-20200414212859-416e5948678a
github.com/IBM/go-sdk-core v1.1.0
Expand Down
17 changes: 17 additions & 0 deletions ibm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/IBM-Cloud/bluemix-go/api/cis/cisv1"
"github.com/IBM-Cloud/bluemix-go/api/container/containerv1"
"github.com/IBM-Cloud/bluemix-go/api/container/containerv2"
registryv1 "github.com/IBM-Cloud/bluemix-go/api/container/registryv1"
"github.com/IBM-Cloud/bluemix-go/api/globalsearch/globalsearchv2"
"github.com/IBM-Cloud/bluemix-go/api/globaltagging/globaltaggingv3"
"github.com/IBM-Cloud/bluemix-go/api/hpcs"
Expand Down Expand Up @@ -172,6 +173,7 @@ type ClientSession interface {
BluemixUserDetails() (*UserConfig, error)
ContainerAPI() (containerv1.ContainerServiceAPI, error)
VpcContainerAPI() (containerv2.ContainerServiceAPI, error)
ContainerRegistryAPI() (registryv1.RegistryServiceAPI, error)
CisAPI() (cisv1.CisServiceAPI, error)
FunctionClient() (*whisk.Client, error)
GlobalSearchAPI() (globalsearchv2.GlobalSearchServiceAPI, error)
Expand Down Expand Up @@ -250,6 +252,9 @@ type clientSession struct {
csv2ConfigErr error
csv2ServiceAPI containerv2.ContainerServiceAPI

crv1ConfigErr error
crv1ServiceAPI registryv1.RegistryServiceAPI

stxConfigErr error
stxServiceAPI schematics.SchematicsServiceAPI

Expand Down Expand Up @@ -464,6 +469,11 @@ func (sess clientSession) VpcContainerAPI() (containerv2.ContainerServiceAPI, er
return sess.csv2ServiceAPI, sess.csv2ConfigErr
}

// ContainerRegistryAPI provides v2Container Service APIs ...
func (sess clientSession) ContainerRegistryAPI() (registryv1.RegistryServiceAPI, error) {
return sess.crv1ServiceAPI, sess.crv1ConfigErr
}

// SchematicsAPI provides schematics Service APIs ...
func (sess clientSession) SchematicsAPI() (schematics.SchematicsServiceAPI, error) {
return sess.stxServiceAPI, sess.stxConfigErr
Expand Down Expand Up @@ -817,6 +827,7 @@ func (c *Config) ClientSession() (interface{}, error) {
session.accountV1ConfigErr = errEmptyBluemixCredentials
session.csConfigErr = errEmptyBluemixCredentials
session.csv2ConfigErr = errEmptyBluemixCredentials
session.crv1ConfigErr = errEmptyBluemixCredentials
session.kpErr = errEmptyBluemixCredentials
session.kmsErr = errEmptyBluemixCredentials
session.stxConfigErr = errEmptyBluemixCredentials
Expand Down Expand Up @@ -943,6 +954,12 @@ func (c *Config) ClientSession() (interface{}, error) {
}
session.csv2ServiceAPI = v2clusterAPI

v1registryAPI, err := registryv1.New(sess.BluemixSession)
if err != nil {
session.crv1ConfigErr = fmt.Errorf("Error occured while configuring Container Registry: %q", err)
}
session.crv1ServiceAPI = v1registryAPI

hpcsAPI, err := hpcs.New(sess.BluemixSession)
if err != nil {
session.hpcsEndpointErr = fmt.Errorf("Error occured while configuring hpcs Endpoint: %q", err)
Expand Down
88 changes: 88 additions & 0 deletions ibm/data_source_ibm_cr_namespaces.go
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
}
31 changes: 31 additions & 0 deletions ibm/data_source_ibm_cr_namespaces_test.go
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" {}
`)
}
3 changes: 3 additions & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func Provider() terraform.ResourceProvider {
"ibm_container_vpc_cluster_worker_pool": dataSourceIBMContainerVpcClusterWorkerPool(),
"ibm_container_vpc_worker_pool": dataSourceIBMContainerVpcClusterWorkerPool(),
"ibm_container_worker_pool": dataSourceIBMContainerWorkerPool(),
"ibm_cr_namespaces": dataIBMContainerRegistryNamespaces(),
"ibm_cos_bucket": dataSourceIBMCosBucket(),
"ibm_dns_domain_registration": dataSourceIBMDNSDomainRegistration(),
"ibm_dns_domain": dataSourceIBMDNSDomain(),
Expand Down Expand Up @@ -379,6 +380,7 @@ func Provider() terraform.ResourceProvider {
"ibm_container_bind_service": resourceIBMContainerBindService(),
"ibm_container_worker_pool": resourceIBMContainerWorkerPool(),
"ibm_container_worker_pool_zone_attachment": resourceIBMContainerWorkerPoolZoneAttachment(),
"ibm_cr_namespace": resourceIBMContainerRegistryNamespace(),
"ibm_cos_bucket": resourceIBMCOS(),
"ibm_dns_domain": resourceIBMDNSDomain(),
"ibm_dns_domain_registration_nameservers": resourceIBMDNSDomainRegistrationNameservers(),
Expand Down Expand Up @@ -530,6 +532,7 @@ func Validator() ValidatorDict {
"ibm_cis_range_app": resourceIBMCISRangeAppValidator(),
"ibm_cis_waf_rule": resourceIBMCISWAFRuleValidator(),
"ibm_cis_certificate_order": resourceIBMCISCertificateOrderValidator(),
"ibm_cr_namespace": resourceIBMCrNamespaceValidator(),
"ibm_tg_gateway": resourceIBMTGValidator(),
"ibm_tg_connection": resourceIBMTransitGatewayConnectionValidator(),
"ibm_dl_virtual_connection": resourceIBMdlGatewayVCValidator(),
Expand Down
Loading

0 comments on commit 4d3eaf9

Please sign in to comment.