github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/satellite.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  	"strings"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  	bluemix "github.com/IBM-Cloud/bluemix-go"
    24  	"github.com/IBM-Cloud/bluemix-go/session"
    25  	"github.com/IBM-Cloud/container-services-go-sdk/kubernetesserviceapiv1"
    26  
    27  	"github.com/IBM/go-sdk-core/v3/core"
    28  )
    29  
    30  type SatelliteGenerator struct {
    31  	IBMService
    32  }
    33  
    34  func (g SatelliteGenerator) loadLocations(locID, locName string) terraformutils.Resource {
    35  	resource := terraformutils.NewResource(
    36  		locID,
    37  		normalizeResourceName(locName, false),
    38  		"ibm_satellite_location",
    39  		"ibm",
    40  		map[string]string{},
    41  		[]string{},
    42  		map[string]interface{}{})
    43  
    44  	// Remove parameters
    45  	resource.IgnoreKeys = append(resource.IgnoreKeys,
    46  		"^labels$",
    47  	)
    48  
    49  	return resource
    50  }
    51  
    52  func (g SatelliteGenerator) loadAssignHostControlPlane(locID, hostID string, labels []string, dependsOn []string) terraformutils.Resource {
    53  	resource := terraformutils.NewResource(
    54  		fmt.Sprintf("%s/%s", locID, hostID),
    55  		normalizeResourceName("ibm_satellite_host", true),
    56  		"ibm_satellite_host",
    57  		"ibm",
    58  		map[string]string{},
    59  		[]string{},
    60  		map[string]interface{}{
    61  			"labels":     labels,
    62  			"depends_on": dependsOn,
    63  		})
    64  	return resource
    65  }
    66  
    67  func (g *SatelliteGenerator) InitResources() error {
    68  	bmxConfig := &bluemix.Config{
    69  		BluemixAPIKey: os.Getenv("IC_API_KEY"),
    70  	}
    71  
    72  	sess, err := session.New(bmxConfig)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	err = authenticateAPIKey(sess)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	bluemixToken := ""
    83  	if strings.HasPrefix(sess.Config.IAMAccessToken, "Bearer") {
    84  		bluemixToken = sess.Config.IAMAccessToken[7:len(sess.Config.IAMAccessToken)]
    85  	} else {
    86  		bluemixToken = sess.Config.IAMAccessToken
    87  	}
    88  
    89  	containerEndpoint := kubernetesserviceapiv1.DefaultServiceURL
    90  	kubernetesServiceV1Options := &kubernetesserviceapiv1.KubernetesServiceApiV1Options{
    91  		URL: envFallBack([]string{"IBMCLOUD_SATELLITE_API_ENDPOINT"}, containerEndpoint),
    92  		Authenticator: &core.BearerTokenAuthenticator{
    93  			BearerToken: bluemixToken,
    94  		},
    95  	}
    96  
    97  	satelliteClient, err := kubernetesserviceapiv1.NewKubernetesServiceApiV1(kubernetesServiceV1Options)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	getSatLocOpts := &kubernetesserviceapiv1.GetSatelliteLocationsOptions{}
   103  	locations, _, err := satelliteClient.GetSatelliteLocations(getSatLocOpts)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	for _, loc := range locations {
   109  		var locDependsOn []string
   110  
   111  		// Location
   112  		if loc.Deployments != nil && !strings.Contains(*loc.Deployments.Message, "R0037") {
   113  			g.Resources = append(g.Resources, g.loadLocations(*loc.ID, *loc.Name))
   114  			resourceName := g.Resources[len(g.Resources)-1:][0].ResourceName
   115  			locDependsOn = append(locDependsOn,
   116  				"ibm_satellite_location."+resourceName)
   117  
   118  			// Assign Host - Control plane
   119  			getSatHostOpts := &kubernetesserviceapiv1.GetSatelliteHostsOptions{
   120  				Controller: loc.ID,
   121  			}
   122  			hosts, resp, err := satelliteClient.GetSatelliteHosts(getSatHostOpts)
   123  			if err != nil {
   124  				return fmt.Errorf("Error getting satellite control plane hosts %s\n%s", err, resp)
   125  			}
   126  
   127  			for _, host := range hosts {
   128  				if *host.Assignment.ClusterName == "infrastructure" {
   129  					hostLabels := []string{}
   130  					for key, value := range host.Labels {
   131  						hostLabels = append(hostLabels, fmt.Sprintf("%s=%s", key, value))
   132  					}
   133  					g.Resources = append(g.Resources, g.loadAssignHostControlPlane(*loc.ID, *host.ID, hostLabels, locDependsOn))
   134  				}
   135  			}
   136  
   137  		}
   138  	}
   139  
   140  	return nil
   141  }