github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/commercetools/shipping_zone.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  
    15  package commercetools
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/labd/commercetools-go-sdk/commercetools"
    23  )
    24  
    25  type ShippingZoneGenerator struct {
    26  	CommercetoolsService
    27  }
    28  
    29  // InitResources generates Terraform Resources from Commercetools API
    30  func (g *ShippingZoneGenerator) InitResources() error {
    31  	cfg := connectivity.Config{
    32  		ClientID:     g.GetArgs()["client_id"].(string),
    33  		ClientSecret: g.GetArgs()["client_secret"].(string),
    34  		ClientScope:  g.GetArgs()["client_scope"].(string),
    35  		TokenURL:     g.GetArgs()["token_url"].(string) + "/oauth/token",
    36  		BaseURL:      g.GetArgs()["base_url"].(string),
    37  	}
    38  
    39  	client := cfg.NewClient()
    40  
    41  	zones, err := client.ZoneQuery(context.Background(), &commercetools.QueryInput{})
    42  	if err != nil {
    43  		return err
    44  	}
    45  	for _, zone := range zones.Results {
    46  		resourceName := zone.Key
    47  		if resourceName == "" {
    48  			resourceName = zone.Name
    49  		}
    50  
    51  		g.Resources = append(g.Resources, terraformutils.NewResource(
    52  			zone.ID,
    53  			resourceName,
    54  			"commercetools_shipping_zone",
    55  			"commercetools",
    56  			map[string]string{},
    57  			[]string{},
    58  			map[string]interface{}{},
    59  		))
    60  	}
    61  	return nil
    62  }