github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/database_redis.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  	"os"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/IBM-Cloud/bluemix-go"
    22  	"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/catalog"
    23  	"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
    24  	"github.com/IBM-Cloud/bluemix-go/session"
    25  )
    26  
    27  // DatabaseRedisGenerator ...
    28  type DatabaseRedisGenerator struct {
    29  	IBMService
    30  }
    31  
    32  // loadRedisDB ...
    33  func (g DatabaseRedisGenerator) loadRedisDB(dbID string, dbName string) terraformutils.Resource {
    34  	resources := terraformutils.NewSimpleResource(
    35  		dbID,
    36  		normalizeResourceName(dbName, false),
    37  		"ibm_database",
    38  		"ibm",
    39  		[]string{})
    40  	return resources
    41  }
    42  
    43  // InitResources ...
    44  func (g *DatabaseRedisGenerator) InitResources() error {
    45  
    46  	region := g.Args["region"].(string)
    47  	bmxConfig := &bluemix.Config{
    48  		BluemixAPIKey: os.Getenv("IC_API_KEY"),
    49  		Region:        region,
    50  	}
    51  	sess, err := session.New(bmxConfig)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	catalogClient, err := catalog.New(sess)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	controllerClient, err := controllerv2.New(sess)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	serviceID, err := catalogClient.ResourceCatalog().FindByName("databases-for-redis", true)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	query := controllerv2.ServiceInstanceQuery{
    71  		ServiceID: serviceID[0].ID,
    72  	}
    73  	redisInstances, err := controllerClient.ResourceServiceInstanceV2().ListInstances(query)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	for _, db := range redisInstances {
    79  		if db.RegionID == region {
    80  			g.Resources = append(g.Resources, g.loadRedisDB(db.ID, db.Name))
    81  		}
    82  	}
    83  
    84  	return nil
    85  }