github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/vpc_cluster.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  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/IBM-Cloud/bluemix-go"
    23  	"github.com/IBM-Cloud/bluemix-go/api/container/containerv2"
    24  	"github.com/IBM-Cloud/bluemix-go/session"
    25  )
    26  
    27  type VPCClusterGenerator struct {
    28  	IBMService
    29  }
    30  
    31  func (g VPCClusterGenerator) loadcluster(clustersID, clusterName string) terraformutils.Resource {
    32  	resource := terraformutils.NewSimpleResource(
    33  		clustersID,
    34  		normalizeResourceName(clusterName, false),
    35  		"ibm_container_vpc_cluster",
    36  		"ibm",
    37  		[]string{})
    38  	return resource
    39  }
    40  
    41  func (g VPCClusterGenerator) loadWorkerPools(clustersID, poolID, poolName string, dependsOn []string) terraformutils.Resource {
    42  	resource := terraformutils.NewResource(
    43  		fmt.Sprintf("%s/%s", clustersID, poolID),
    44  		normalizeResourceName(poolName, true),
    45  		"ibm_container_vpc_worker_pool",
    46  		"ibm",
    47  		map[string]string{},
    48  		[]string{},
    49  		map[string]interface{}{
    50  			"depends_on": dependsOn,
    51  		})
    52  	return resource
    53  }
    54  
    55  func (g *VPCClusterGenerator) InitResources() error {
    56  	bmxConfig := &bluemix.Config{
    57  		BluemixAPIKey: os.Getenv("IC_API_KEY"),
    58  	}
    59  	sess, err := session.New(bmxConfig)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	client, err := containerv2.New(sess)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	clusters, err := client.Clusters().List(containerv2.ClusterTargetHeader{})
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	for _, cs := range clusters {
    74  		g.Resources = append(g.Resources, g.loadcluster(cs.ID, cs.Name))
    75  		resourceName := g.Resources[len(g.Resources)-1:][0].ResourceName
    76  		workerPools, err := client.WorkerPools().ListWorkerPools(cs.ID, containerv2.ClusterTargetHeader{})
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		for _, pool := range workerPools {
    82  			if pool.PoolName != "default" {
    83  				var dependsOn []string
    84  				dependsOn = append(dependsOn,
    85  					"ibm_container_vpc_cluster."+resourceName)
    86  				g.Resources = append(g.Resources, g.loadWorkerPools(cs.ID, pool.ID, pool.PoolName, dependsOn))
    87  			}
    88  		}
    89  
    90  	}
    91  	return nil
    92  }