Skip to content

Commit

Permalink
Add commercetools custom_object resource
Browse files Browse the repository at this point in the history
  • Loading branch information
saaguero committed Apr 19, 2021
1 parent 6e9500b commit 8631165
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 33 deletions.
64 changes: 42 additions & 22 deletions docs/commercetools.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,50 @@ This provider use the [terraform-provider-commercetools](https://github.com/labd

Example:

Export required variables:

```bash
export CTP_PROJECT_KEY=key
export CTP_CLIENT_ID=foo
export CTP_CLIENT_SECRET=bar
export CTP_CLIENT_SCOPE=scope
```

Export optional variables in case default values are not appropiate:

```bash
export CTP_BASE_URL=base_url # default: https://api.sphere.io
export CTP_TOKEN_URL=token_url # default: https://auth.sphere.io
```
CTP_CLIENT_ID=foo CTP_CLIENT_SCOPE=scope CTP_CLIENT_SECRET=bar CTP_PROJECT_KEY=key ./terraformer plan commercetools -r=types // Only planning
CTP_CLIENT_ID=foo CTP_CLIENT_SCOPE=scope CTP_CLIENT_SECRET=bar CTP_PROJECT_KEY=key ./terraformer import commercetools -r=types // Import commercetools types

Run terraformer

```bash
./terraformer plan commercetools -r=types # Only planning
./terraformer import commercetools -r=types # Import commercetools types
```

List of supported [commercetools](https://commercetools.com/de/) resources:

* `api_extension`
* `commercetools_api_extension`
* `channel`
* `commercetools_channel`
* `product_type`
* `commercetools_product_type`
* `shipping_method`
* `commercetools_shipping_method`
* `shipping_zone`
* `commercetools_shipping_zone`
* `state`
* `commercetools_state`
* `store`
* `commercetools_store`
* `subscription`
* `commercetools_subscription`
* `tax_category`
* `commercetools_tax_category`
* `types`
* `commercetools_type`
- `api_extension`
- `commercetools_api_extension`
- `channel`
- `commercetools_channel`
- `custom_object`
- `commercetools_custom_object`
- `product_type`
- `commercetools_product_type`
- `shipping_method`
- `commercetools_shipping_method`
- `shipping_zone`
- `commercetools_shipping_zone`
- `state`
- `commercetools_state`
- `store`
- `commercetools_store`
- `subscription`
- `commercetools_subscription`
- `tax_category`
- `commercetools_tax_category`
- `types`
- `commercetools_type`
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ require (
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/jmespath/go-jmespath v0.4.0
github.com/jonboydell/logzio_client v1.2.0
github.com/labd/commercetools-go-sdk v0.0.0-20200309143931-ca72e918a79d
github.com/labd/commercetools-go-sdk v0.3.1
github.com/linode/linodego v0.24.1
github.com/mrparkers/terraform-provider-keycloak v0.0.0-20200506151941-509881368409
github.com/nicksnyder/go-i18n v1.10.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/
github.com/apparentlymart/go-textseg/v12 v12.0.0 h1:bNEQyAGak9tojivJNkoqWErVCQbjdL7GzRt3F8NvfJ0=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-versions v0.0.2-0.20180815153302-64b99f7cb171/go.mod h1:JXY95WvQrPJQtudvNARshgWajS7jNNlM90altXIPNyI=
github.com/appscode/go-querystring v0.0.0-20170504095604-0126cfb3f1dc h1:LoL75er+LKDHDUfU5tRvFwxH0LjPpZN8OoG8Ll+liGU=
github.com/appscode/go-querystring v0.0.0-20170504095604-0126cfb3f1dc/go.mod h1:w648aMHEgFYS6xb0KVMMtZ2uMeemhiKCuD2vj6gY52A=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down Expand Up @@ -373,6 +375,7 @@ github.com/cucumber/gherkin-go/v13 v13.0.0 h1:d09AwPZldyOFlCADAIZQMt7T+VHCrGdp51
github.com/cucumber/gherkin-go/v13 v13.0.0/go.mod h1:pzMEPEIPcPOBDkE8HaWC+Ck6eDBtBmIWVksUkSin/2E=
github.com/cucumber/messages-go/v12 v12.0.0 h1:ZlZYZrYDDc35zCe0HK2y95GUcxHHqr8yB9kdrXCjnzU=
github.com/cucumber/messages-go/v12 v12.0.0/go.mod h1:5zuJu21U6rB+BBqyGoQr839a4h4GUElPnSozdHyhCOQ=
github.com/dave/jennifer v1.4.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -752,6 +755,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labd/commercetools-go-sdk v0.0.0-20200309143931-ca72e918a79d h1:wI9Pc33M4Weg78J/fDFsKWTiEsAvriH6x4EHbcR3zus=
github.com/labd/commercetools-go-sdk v0.0.0-20200309143931-ca72e918a79d/go.mod h1:uOXGd793oxD6CZiPp30hzs4L511bcYSF0yPu17LBqAo=
github.com/labd/commercetools-go-sdk v0.3.1 h1:Fk8/VKKDSNHhyVhDywJdmzUt3JSpNoFZbHJndwjVBls=
github.com/labd/commercetools-go-sdk v0.3.1/go.mod h1:I+KKNALlg6PcSertsVA7E442koO99GT7gldWqwZlUGo=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -817,6 +822,7 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/panicwrap v1.0.0/go.mod h1:pKvZHwWrZowLUzftuFq7coarnxbBXU4aQh3N0BJOeeA=
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/api_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *APIExtensionGenerator) InitResources() error {

client := cfg.NewClient()

extensions, err := client.ExtensionQuery(&commercetools.QueryInput{})
extensions, err := client.ExtensionQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *ChannelGenerator) InitResources() error {

client := cfg.NewClient()

channels, err := client.ChannelQuery(&commercetools.QueryInput{})
channels, err := client.ChannelQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions providers/commercetools/commercetools_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (p *CommercetoolsProvider) GetSupportedService() map[string]terraformutils.
return map[string]terraformutils.ServiceGenerator{
"api_extension": &APIExtensionGenerator{},
"channel": &ChannelGenerator{},
"custom_object": &CustomObjectGenerator{},
"product_type": &ProductTypeGenerator{},
"shipping_zone": &ShippingZoneGenerator{},
"shipping_method": &ShippingMethodGenerator{},
Expand Down
57 changes: 57 additions & 0 deletions providers/commercetools/custom_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2018 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
)

type CustomObjectGenerator struct {
CommercetoolsService
}

// InitResources generates Terraform Resources from Commercetools API
func (g *CustomObjectGenerator) InitResources() error {
cfg := connectivity.Config{
ClientID: g.GetArgs()["client_id"].(string),
ClientSecret: g.GetArgs()["client_secret"].(string),
ClientScope: g.GetArgs()["client_scope"].(string),
TokenURL: g.GetArgs()["token_url"].(string) + "/oauth/token",
BaseURL: g.GetArgs()["base_url"].(string),
}

client := cfg.NewClient()

customObjects, err := client.CustomObjectQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
for _, customObject := range customObjects.Results {
g.Resources = append(g.Resources, terraformutils.NewResource(
customObject.ID,
customObject.Key,
"commercetools_custom_object",
"commercetools",
map[string]string{},
[]string{},
map[string]interface{}{},
))
}
return nil
}
4 changes: 3 additions & 1 deletion providers/commercetools/product_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *ProductTypeGenerator) InitResources() error {

client := cfg.NewClient()

productTypes, err := client.ProductTypeQuery(&commercetools.QueryInput{})
productTypes, err := client.ProductTypeQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/shipping_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *ShippingMethodGenerator) InitResources() error {

client := cfg.NewClient()

zones, err := client.ShippingMethodQuery(&commercetools.QueryInput{})
zones, err := client.ShippingMethodQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/shipping_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *ShippingZoneGenerator) InitResources() error {

client := cfg.NewClient()

zones, err := client.ZoneQuery(&commercetools.QueryInput{})
zones, err := client.ZoneQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *StateGenerator) InitResources() error {

client := cfg.NewClient()

states, err := client.StateQuery(&commercetools.QueryInput{})
states, err := client.StateQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *StoreGenerator) InitResources() error {

client := cfg.NewClient()

stores, err := client.StoreQuery(&commercetools.QueryInput{})
stores, err := client.StoreQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *SubscriptionGenerator) InitResources() error {

client := cfg.NewClient()

subscriptions, err := client.SubscriptionQuery(&commercetools.QueryInput{})
subscriptions, err := client.SubscriptionQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/tax_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *TaxCategoryGenerator) InitResources() error {

client := cfg.NewClient()

categories, err := client.TaxCategoryQuery(&commercetools.QueryInput{})
categories, err := client.TaxCategoryQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion providers/commercetools/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commercetools

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/labd/commercetools-go-sdk/commercetools"
Expand All @@ -36,7 +38,7 @@ func (g *TypesGenerator) InitResources() error {

client := cfg.NewClient()

types, err := client.TypeQuery(&commercetools.QueryInput{})
types, err := client.TypeQuery(context.Background(), &commercetools.QueryInput{})
if err != nil {
return err
}
Expand Down

0 comments on commit 8631165

Please sign in to comment.