Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#525 from chenrui333/add-fastly…
Browse files Browse the repository at this point in the history
…-user-support

Add fastly user support
  • Loading branch information
sergeylanzman authored Jun 7, 2020
2 parents 8a5db02 + 14b4717 commit 476fac8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Links to download Terraform Providers:
* Alicloud provider >1.57.1 - [here](https://releases.hashicorp.com/terraform-provider-alicloud/)
* Cloud
* DigitalOcean provider >1.9.1 - [here](https://releases.hashicorp.com/terraform-provider-digitalocean/)
* Fastly provider >0.11.0 - [here](https://releases.hashicorp.com/terraform-provider-fastly/)
* Fastly provider >0.16.1 - [here](https://releases.hashicorp.com/terraform-provider-fastly/)
* Heroku provider >2.2.1 - [here](https://releases.hashicorp.com/terraform-provider-heroku/)
* Linode provider >1.8.0 - [here](https://releases.hashicorp.com/terraform-provider-linode/)
* OpenStack provider >1.21.1 - [here](https://releases.hashicorp.com/terraform-provider-openstack/)
Expand Down Expand Up @@ -832,7 +832,8 @@ Example:

```
export FASTLY_API_KEY=[FASTLY_API_KEY]
./terraformer import fastly -r service_v1
export FASTLY_CUSTOMER_ID=[FASTLY_CUSTOMER_ID]
./terraformer import fastly -r service_v1,user
```

List of supported Fastly resources:
Expand All @@ -842,6 +843,8 @@ List of supported Fastly resources:
* `fastly_service_dictionary_items_v1`
* `fastly_service_dynamic_snippet_content_v1`
* `fastly_service_v1`
* `user`
* `fastly_user_v1`

### Use with Heroku

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/denverdino/aliyungo v0.0.0-20200327235253-d59c209c7e93
github.com/digitalocean/godo v1.35.1
github.com/dollarshaveclub/new-relic-synthetics-go v0.0.0-20170605224734-4dc3dd6ae884
github.com/fastly/go-fastly v1.10.0
github.com/fastly/go-fastly v1.15.0
github.com/google/go-github/v25 v25.1.3
github.com/gophercloud/gophercloud v0.10.0
github.com/hashicorp/go-azure-helpers v0.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fastly/go-fastly v1.10.0 h1:L9RgDIj2AV2CKQap1ZSFPoT3Ds/R6Hs52doJmFAjTlg=
github.com/fastly/go-fastly v1.10.0/go.mod h1:jILbTQnU/K/7XHQNzQWd1O7hbXIcp6dKrxfRWqU6xfk=
github.com/fastly/go-fastly v1.15.0 h1:t7f0ZnQy0rKYN8FCql4wHkfZNHajxDs1hT/phazOpp8=
github.com/fastly/go-fastly v1.15.0/go.mod h1:jILbTQnU/K/7XHQNzQWd1O7hbXIcp6dKrxfRWqU6xfk=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down
15 changes: 12 additions & 3 deletions providers/fastly/fastly_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (

type FastlyProvider struct { //nolint
terraformutils.Provider
apiKey string
customerID string
apiKey string
}

func (p *FastlyProvider) Init(args []string) error {
Expand All @@ -33,6 +34,11 @@ func (p *FastlyProvider) Init(args []string) error {
}
p.apiKey = os.Getenv("FASTLY_API_KEY")

if os.Getenv("FASTLY_CUSTOMER_ID") == "" {
return errors.New("set FASTLY_CUSTOMER_ID env var")
}
p.customerID = os.Getenv("FASTLY_CUSTOMER_ID")

return nil
}

Expand All @@ -44,7 +50,8 @@ func (p *FastlyProvider) GetProviderData(arg ...string) map[string]interface{} {
return map[string]interface{}{
"provider": map[string]interface{}{
"fastly": map[string]interface{}{
"version": providerwrapper.GetProviderVersion(p.GetName()),
"version": providerwrapper.GetProviderVersion(p.GetName()),
"customer_id": p.customerID,
},
},
}
Expand All @@ -57,6 +64,7 @@ func (FastlyProvider) GetResourceConnections() map[string]map[string][]string {
func (p *FastlyProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
return map[string]terraformutils.ServiceGenerator{
"service_v1": &ServiceV1Generator{},
"user": &UserGenerator{},
}
}

Expand All @@ -70,7 +78,8 @@ func (p *FastlyProvider) InitService(serviceName string, verbose bool) error {
p.Service.SetVerbose(verbose)
p.Service.SetProviderName(p.GetName())
p.Service.SetArgs(map[string]interface{}{
"api_key": p.apiKey,
"customer_id": p.customerID,
"api_key": p.apiKey,
})
return nil
}
53 changes: 53 additions & 0 deletions providers/fastly/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019 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 fastly

import (
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/fastly/go-fastly/fastly"
)

type UserGenerator struct {
FastlyService
}

func (g *UserGenerator) loadUsers(client *fastly.Client, customerID string) error {
users, err := client.ListCustomerUsers(&fastly.ListCustomerUsersInput{CustomerID: customerID})
if err != nil {
return err
}
for _, user := range users {
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
user.ID,
user.ID,
"fastly_user_v1",
"fastly",
[]string{}))
}
return nil
}

func (g *UserGenerator) InitResources() error {
client, err := fastly.NewClient(g.Args["api_key"].(string))
if err != nil {
return err
}

if err := g.loadUsers(client, g.Args["customer_id"].(string)); err != nil {
return err
}

return nil
}

0 comments on commit 476fac8

Please sign in to comment.