github.com/GoogleCloudPlatform/terraformer@v0.8.18/cmd/provider_cmd_commercetools.go (about)

     1  // Copyright 2018 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  package cmd
    15  
    16  import (
    17  	"errors"
    18  	"os"
    19  
    20  	commercetools_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/commercetools"
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  const (
    26  	defaultCommercetoolsBaseURL  = "https://api.sphere.io"
    27  	defaultCommercetoolsTokenURL = "https://auth.sphere.io"
    28  )
    29  
    30  func newCmdCommercetoolsImporter(options ImportOptions) *cobra.Command {
    31  	cmd := &cobra.Command{
    32  		Use:   "commercetools",
    33  		Short: "Import current state to Terraform configuration from Commercetools",
    34  		Long:  "Import current state to Terraform configuration from Commercetools",
    35  		RunE: func(cmd *cobra.Command, args []string) error {
    36  			clientID := os.Getenv("CTP_CLIENT_ID")
    37  			if len(clientID) == 0 {
    38  				return errors.New("API client ID for commercetools must be set through `CTP_CLIENT_ID` env var")
    39  			}
    40  			clientScope := os.Getenv("CTP_CLIENT_SCOPE")
    41  			if len(clientScope) == 0 {
    42  				return errors.New("API client scope for comercetools must be set through `CTP_CLIENT_SCOPE` env var")
    43  			}
    44  			clientSecret := os.Getenv("CTP_CLIENT_SECRET")
    45  			if len(clientSecret) == 0 {
    46  				return errors.New("API client secret for comercetools must be set through `CTP_CLIENT_SECRET` env var")
    47  			}
    48  			projectKey := os.Getenv("CTP_PROJECT_KEY")
    49  			if len(projectKey) == 0 {
    50  				return errors.New("API project key for comercetools must be set through `CTP_PROJECT_KEY` env var")
    51  			}
    52  			baseURL := os.Getenv("CTP_BASE_URL")
    53  			if len(baseURL) == 0 {
    54  				baseURL = defaultCommercetoolsBaseURL
    55  			}
    56  			tokenURL := os.Getenv("CTP_TOKEN_URL")
    57  			if len(tokenURL) == 0 {
    58  				tokenURL = defaultCommercetoolsTokenURL
    59  			}
    60  			provider := newCommercetoolsProvider()
    61  			err := Import(provider, options, []string{clientID, clientScope, clientSecret, projectKey, baseURL, tokenURL})
    62  			if err != nil {
    63  				return err
    64  			}
    65  			return nil
    66  		},
    67  	}
    68  	cmd.AddCommand(listCmd(newCommercetoolsProvider()))
    69  	baseProviderFlags(cmd.PersistentFlags(), &options, "types", "type=id1:id2:id4")
    70  	return cmd
    71  }
    72  
    73  func newCommercetoolsProvider() terraformutils.ProviderGenerator {
    74  	return &commercetools_terraforming.CommercetoolsProvider{}
    75  }