-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from StarpTech/feature/add_commercetools_prov…
…ider Add commercetools provider
- Loading branch information
Showing
20 changed files
with
902 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// 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 cmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
commercetools_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/commercetools" | ||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
defaultCommercetoolsBaseURL = "https://api.sphere.io" | ||
defaultCommercetoolsTokenURL = "https://auth.sphere.io" | ||
) | ||
|
||
func newCmdCommercetoolsImporter(options ImportOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "commercetools", | ||
Short: "Import current state to Terraform configuration from Commercetools", | ||
Long: "Import current state to Terraform configuration from Commercetools", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientID := os.Getenv("CTP_CLIENT_ID") | ||
if len(clientID) == 0 { | ||
return errors.New("API client ID for commercetools must be set through `CTP_CLIENT_ID` env var") | ||
} | ||
clientScope := os.Getenv("CTP_CLIENT_SCOPE") | ||
if len(clientScope) == 0 { | ||
return errors.New("API client scope for comercetools must be set through `CTP_CLIENT_SCOPE` env var") | ||
} | ||
clientSecret := os.Getenv("CTP_CLIENT_SECRET") | ||
if len(clientSecret) == 0 { | ||
return errors.New("API client secret for comercetools must be set through `CTP_CLIENT_SECRET` env var") | ||
} | ||
projectKey := os.Getenv("CTP_PROJECT_KEY") | ||
if len(projectKey) == 0 { | ||
return errors.New("API project key for comercetools must be set through `CTP_PROJECT_KEY` env var") | ||
} | ||
baseURL := os.Getenv("CTP_BASE_URL") | ||
if len(baseURL) == 0 { | ||
baseURL = defaultCommercetoolsBaseURL | ||
} | ||
tokenURL := os.Getenv("CTP_TOKEN_URL") | ||
if len(tokenURL) == 0 { | ||
tokenURL = defaultCommercetoolsTokenURL | ||
} | ||
provider := newCommercetoolsProvider() | ||
err := Import(provider, options, []string{clientID, clientScope, clientSecret, projectKey, baseURL, tokenURL}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.AddCommand(listCmd(newCommercetoolsProvider())) | ||
cmd.PersistentFlags().BoolVarP(&options.Connect, "connect", "c", true, "") | ||
cmd.PersistentFlags().StringSliceVarP(&options.Resources, "resources", "r", []string{}, "types") | ||
cmd.PersistentFlags().StringVarP(&options.PathPattern, "path-pattern", "p", DefaultPathPattern, "{output}/{provider}/custom/{service}/") | ||
cmd.PersistentFlags().StringVarP(&options.PathOutput, "path-output", "o", DefaultPathOutput, "") | ||
cmd.PersistentFlags().StringVarP(&options.State, "state", "s", DefaultState, "local or bucket") | ||
cmd.PersistentFlags().StringVarP(&options.Bucket, "bucket", "b", "", "gs://terraform-state") | ||
cmd.PersistentFlags().StringSliceVarP(&options.Filter, "filter", "f", []string{}, "commercetools_type=id1:id2:id4") | ||
return cmd | ||
} | ||
|
||
func newCommercetoolsProvider() terraform_utils.ProviderGenerator { | ||
return &commercetools_terraforming.CommercetoolsProvider{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 ( | ||
"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity" | ||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils" | ||
"github.com/labd/commercetools-go-sdk/commercetools" | ||
) | ||
|
||
type ApiExtensionGenerator struct { | ||
CommercetoolsService | ||
} | ||
|
||
// InitResources generates Terraform Resources from Commercetools API | ||
func (g *ApiExtensionGenerator) 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() | ||
|
||
extensions, err := client.ExtensionQuery(&commercetools.QueryInput{}) | ||
if err != nil { | ||
return err | ||
} | ||
for _, extension := range extensions.Results { | ||
g.Resources = append(g.Resources, terraform_utils.NewResource( | ||
extension.ID, | ||
extension.Key, | ||
"commercetools_api_extension", | ||
"commercetools", | ||
map[string]string{}, | ||
[]string{}, | ||
map[string]interface{}{}, | ||
)) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 ( | ||
"github.com/GoogleCloudPlatform/terraformer/providers/commercetools/connectivity" | ||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils" | ||
"github.com/labd/commercetools-go-sdk/commercetools" | ||
) | ||
|
||
type ChannelGenerator struct { | ||
CommercetoolsService | ||
} | ||
|
||
// InitResources generates Terraform Resources from Commercetools API | ||
func (g *ChannelGenerator) 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() | ||
|
||
channels, err := client.ChannelQuery(&commercetools.QueryInput{}) | ||
if err != nil { | ||
return err | ||
} | ||
for _, channel := range channels.Results { | ||
g.Resources = append(g.Resources, terraform_utils.NewResource( | ||
channel.ID, | ||
channel.Key, | ||
"commercetools_channel", | ||
"commercetools", | ||
map[string]string{}, | ||
[]string{}, | ||
map[string]interface{}{}, | ||
)) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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 ( | ||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils" | ||
"github.com/GoogleCloudPlatform/terraformer/terraform_utils/provider_wrapper" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type CommercetoolsProvider struct { | ||
terraform_utils.Provider | ||
clientID string | ||
clientSecret string | ||
clientScope string | ||
projectKey string | ||
baseURL string | ||
tokenURL string | ||
} | ||
|
||
func (p CommercetoolsProvider) GetResourceConnections() map[string]map[string][]string { | ||
return map[string]map[string][]string{} | ||
} | ||
|
||
func (p CommercetoolsProvider) GetProviderData(arg ...string) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"provider": map[string]interface{}{ | ||
"commercetools": map[string]interface{}{ | ||
"version": provider_wrapper.GetProviderVersion(p.GetName()), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Init CommerectoolsProvider | ||
func (p *CommercetoolsProvider) Init(args []string) error { | ||
p.clientID = args[0] | ||
p.clientScope = args[1] | ||
p.clientSecret = args[2] | ||
p.projectKey = args[3] | ||
p.baseURL = args[4] | ||
p.tokenURL = args[5] | ||
return nil | ||
} | ||
|
||
func (p *CommercetoolsProvider) GetName() string { | ||
return "commercetools" | ||
} | ||
|
||
func (p *CommercetoolsProvider) InitService(serviceName string) error { | ||
var isSupported bool | ||
if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported { | ||
return errors.New(p.GetName() + ": " + serviceName + " not supported service") | ||
} | ||
p.Service = p.GetSupportedService()[serviceName] | ||
p.Service.SetName(serviceName) | ||
p.Service.SetProviderName(p.GetName()) | ||
p.Service.SetArgs(map[string]interface{}{ | ||
"client_id": p.clientID, | ||
"client_secret": p.clientSecret, | ||
"client_scope": p.clientScope, | ||
"project_key": p.projectKey, | ||
"base_url": p.baseURL, | ||
"token_url": p.tokenURL, | ||
}) | ||
return nil | ||
} | ||
|
||
// GetSupportedService return map of support service for Logzio | ||
func (p *CommercetoolsProvider) GetSupportedService() map[string]terraform_utils.ServiceGenerator { | ||
return map[string]terraform_utils.ServiceGenerator{ | ||
"types": &TypesGenerator{}, | ||
"product_type": &ProductTypeGenerator{}, | ||
"store": &StoreGenerator{}, | ||
"api_extension": &ApiExtensionGenerator{}, | ||
"channel": &ChannelGenerator{}, | ||
"subscription": &SubscriptionGenerator{}, | ||
"shipping_zone": &ShippingZoneGenerator{}, | ||
"state": &StateGenerator{}, | ||
"tax_category": &TaxCategoryGenerator{}, | ||
} | ||
} |
Oops, something went wrong.