github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/cos.go (about)

     1  // Copyright 2019 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ibm
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"regexp"
    21  	"strings"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  	bluemix "github.com/IBM-Cloud/bluemix-go"
    25  	"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/catalog"
    26  	"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
    27  	"github.com/IBM-Cloud/bluemix-go/session"
    28  	"github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam"
    29  
    30  	ibmaws "github.com/IBM/ibm-cos-sdk-go/aws"
    31  	cossession "github.com/IBM/ibm-cos-sdk-go/aws/session"
    32  	coss3 "github.com/IBM/ibm-cos-sdk-go/service/s3"
    33  )
    34  
    35  type COSGenerator struct {
    36  	IBMService
    37  }
    38  
    39  func (g COSGenerator) loadCOS(cosID string, cosName string) terraformutils.Resource {
    40  	resources := terraformutils.NewSimpleResource(
    41  		cosID,
    42  		normalizeResourceName(cosName, false),
    43  		"ibm_resource_instance",
    44  		"ibm",
    45  		[]string{})
    46  	return resources
    47  }
    48  
    49  func (g COSGenerator) loadCOSBuckets(bucketID, bucketName string, dependsOn []string) terraformutils.Resource {
    50  	resources := terraformutils.NewResource(
    51  		bucketID,
    52  		normalizeResourceName(bucketName, false),
    53  		"ibm_cos_bucket",
    54  		"ibm",
    55  		map[string]string{},
    56  		[]string{},
    57  		map[string]interface{}{
    58  			"depends_on": dependsOn,
    59  		})
    60  	return resources
    61  }
    62  
    63  func (g *COSGenerator) InitResources() error {
    64  	bmxConfig := &bluemix.Config{
    65  		BluemixAPIKey: os.Getenv("IC_API_KEY"),
    66  	}
    67  	sess, err := session.New(bmxConfig)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	catalogClient, err := catalog.New(sess)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	controllerClient, err := controllerv2.New(sess)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	serviceID, err := catalogClient.ResourceCatalog().FindByName("cloud-object-storage", true)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	query := controllerv2.ServiceInstanceQuery{
    87  		ServiceID: serviceID[0].ID,
    88  	}
    89  	cosInstances, err := controllerClient.ResourceServiceInstanceV2().ListInstances(query)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	authEndpoint := "https://iam.cloud.ibm.com/identity/token"
    94  	for _, cs := range cosInstances {
    95  		g.Resources = append(g.Resources, g.loadCOS(cs.ID, cs.Name))
    96  		csResourceName := g.Resources[len(g.Resources)-1:][0].ResourceName
    97  		s3Conf := ibmaws.NewConfig().WithCredentials(ibmiam.NewStaticCredentials(ibmaws.NewConfig(), authEndpoint, os.Getenv("IC_API_KEY"), cs.ID)).WithS3ForcePathStyle(true).WithEndpoint("s3.us-south.cloud-object-storage.appdomain.cloud")
    98  		s3Sess := cossession.Must(cossession.NewSession())
    99  		s3Client := coss3.New(s3Sess, s3Conf)
   100  		singleSiteLocationRegex := regexp.MustCompile("^[a-z]{3}[0-9][0-9]-[a-z]{4,8}$")
   101  		regionLocationRegex := regexp.MustCompile("^[a-z]{2}-[a-z]{2,5}-[a-z]{4,8}$")
   102  		crossRegionLocationRegex := regexp.MustCompile("^[a-z]{2}-[a-z]{4,8}$")
   103  		d, _ := s3Client.ListBucketsExtended(&coss3.ListBucketsExtendedInput{})
   104  		for _, b := range d.Buckets {
   105  			var dependsOn []string
   106  			dependsOn = append(dependsOn,
   107  				"ibm_resource_instance."+csResourceName)
   108  			var apiType, location string
   109  			bLocationConstraint := *b.LocationConstraint
   110  			if singleSiteLocationRegex.MatchString(bLocationConstraint) {
   111  				apiType = "ss1"
   112  				location = strings.Split(bLocationConstraint, "-")[0]
   113  			}
   114  			if regionLocationRegex.MatchString(bLocationConstraint) {
   115  				apiType = "rl"
   116  				location = fmt.Sprintf("%s-%s", strings.Split(bLocationConstraint, "-")[0], strings.Split(bLocationConstraint, "-")[1])
   117  			}
   118  			if crossRegionLocationRegex.MatchString(bLocationConstraint) {
   119  				apiType = "crl"
   120  				location = strings.Split(bLocationConstraint, "-")[0]
   121  			}
   122  			bucketID := fmt.Sprintf("%s:%s:%s:meta:%s:%s", strings.ReplaceAll(cs.ID, "::", ""), "bucket", *b.Name, apiType, location)
   123  			g.Resources = append(g.Resources, g.loadCOSBuckets(bucketID, *b.Name, dependsOn))
   124  		}
   125  	}
   126  
   127  	return nil
   128  }