Skip to content

Commit 155d2f4

Browse files
authored
Merge pull request #4 from fnaoto/2020-12-21
Add app collaborator.
2 parents 7596b91 + d5938b4 commit 155d2f4

20 files changed

+586
-37
lines changed

.env.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export DG_API_KEY=""
2+
export DG_USER_NAME=""
3+
export TF_VAR_app_id=""
4+
export TF_VAR_owner=""
5+
export TF_VAR_platform=""
6+
export TF_VAR_add_user_name=""

.envrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export GO111MODULE=on
2+
3+
source .env

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ terraform-provider-deploygate
1414

1515
# Dependency directories (remove the comment below to include it)
1616
# vendor/
17+
18+
# dotenv
19+
.env
20+
21+
# log
22+
crash.log
23+
24+
# terraform
25+
.terraform
26+
*.tfstate
27+
*.tfstate.*

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ release:
2626
GOOS=windows GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_windows_amd64
2727

2828
install: build
29-
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
30-
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
29+
mkdir -p ~/.terraform.d/plugins/${OS_ARCH}
30+
mv ${BINARY} ~/.terraform.d/plugins/${OS_ARCH}
3131

3232
test:
3333
go test -i $(TEST) || exit 1
3434
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
3535

36-
testacc:
36+
testacc: install
3737
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
38+
39+
examples: install
40+
terraform init examples
41+
terraform plan examples
42+
terraform apply examples
43+
terraform destroy examples

deploygate/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package deploygate
2+
3+
import (
4+
go_deploygate "github.com/recruit-mp/go-deploygate"
5+
)
6+
7+
// Config : configuration for deploygate client
8+
type Config struct {
9+
apiKey string
10+
}
11+
12+
// Client : client for deploygate
13+
type Client struct {
14+
client *go_deploygate.Client
15+
}
16+
17+
// Client : API Client for deploygate
18+
func (c *Config) Client() (interface{}, error) {
19+
var clnt Client
20+
21+
client, err := go_deploygate.NewClient(c.apiKey)
22+
clnt.client = client
23+
return &clnt, err
24+
}

deploygate/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package deploygate
2+
3+
import "testing"
4+
5+
func Test_Client(t *testing.T) {
6+
config := &Config{
7+
apiKey: "api key",
8+
}
9+
10+
client, err := config.Client()
11+
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
16+
if client == nil {
17+
t.Fatalf("Nil Client.")
18+
}
19+
}

deploygate/data_source_app_collaborator.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package deploygate
22

33
import (
44
"fmt"
5+
"log"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
78
go_deploygate "github.com/recruit-mp/go-deploygate"
@@ -24,26 +25,77 @@ func dataSourceAppCollaborator() *schema.Resource {
2425
Type: schema.TypeString,
2526
Required: true,
2627
},
28+
"users": {
29+
Type: schema.TypeSet,
30+
Computed: true,
31+
Elem: &schema.Resource{
32+
Schema: map[string]*schema.Schema{
33+
"name": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
},
37+
"role": {
38+
Type: schema.TypeInt,
39+
Optional: true,
40+
},
41+
},
42+
},
43+
},
44+
"teams": {
45+
Type: schema.TypeSet,
46+
Computed: true,
47+
Elem: &schema.Resource{
48+
Schema: map[string]*schema.Schema{
49+
"name": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
},
53+
"role": {
54+
Type: schema.TypeInt,
55+
Optional: true,
56+
},
57+
},
58+
},
59+
},
60+
"usage": {
61+
Type: schema.TypeMap,
62+
Computed: true,
63+
Elem: schema.TypeInt,
64+
},
2765
},
2866
}
2967
}
3068

3169
func dataSourceAppCollaboratorRead(d *schema.ResourceData, meta interface{}) error {
32-
client := go_deploygate.DefaultClient()
70+
client := meta.(*Client).client
3371

3472
owner := d.Get("owner").(string)
3573
platform := d.Get("platform").(string)
3674
appID := d.Get("app_id").(string)
3775

76+
log.Printf("[DEBUG] dataSourceAppCollaboratorRead: %s, %s, %s", owner, platform, appID)
77+
3878
g := &go_deploygate.GetAppCollaboratorInput{
3979
Owner: owner,
4080
Platform: platform,
4181
AppId: appID,
4282
}
43-
collaborator, _ := client.GetAppCollaborator(g)
83+
84+
collaborator, err := client.GetAppCollaborator(g)
85+
86+
if err != nil {
87+
return err
88+
}
89+
90+
rs := collaborator.Results
91+
4492
d.SetId(fmt.Sprintf("%s/%s/%s", owner, platform, appID))
45-
d.Set("users", collaborator.Results.Users)
46-
d.Set("teams", collaborator.Results.Teams)
47-
d.Set("usage", collaborator.Results.Usage)
93+
d.Set("users", rs.Users)
94+
d.Set("teams", rs.Teams)
95+
d.Set("usage", map[string]interface{}{
96+
"max": rs.Usage.Max,
97+
"used": rs.Usage.Used,
98+
})
99+
48100
return nil
49101
}

deploygate/data_source_app_collaborator_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,64 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
99
)
1010

11-
func TestAppCollaborator_basic(t *testing.T) {
11+
func Test_DataSourceAppCollaborator_basic(t *testing.T) {
1212
resource.ParallelTest(t, resource.TestCase{
13-
PreCheck: func() { testDGPreCheck(t) },
13+
PreCheck: func() { Test_DGPreCheck(t) },
1414
Providers: testDGProviders,
1515
Steps: []resource.TestStep{
1616
{
17-
Config: testAppCollaboratorConfig,
17+
Config: testDataSourceAppCollaboratorConfig,
1818
Check: resource.ComposeTestCheckFunc(
19-
testAppCollaborator("data.deploygate_app_collaborator.current"),
19+
testDataSourceAppCollaborator("data.deploygate_app_collaborator.current"),
2020
),
2121
},
2222
},
2323
})
2424
}
2525

26-
func testAppCollaborator(n string) resource.TestCheckFunc {
26+
func testDataSourceAppCollaborator(n string) resource.TestCheckFunc {
2727
return func(s *terraform.State) error {
2828
rs, ok := s.RootModule().Resources[n]
2929
if !ok {
3030
return fmt.Errorf("Can't find app users resource: %s", n)
3131
}
3232

3333
if rs.Primary.ID == "" {
34-
return fmt.Errorf("Account Id resource ID not set")
34+
return fmt.Errorf("Resource ID not set")
3535
}
3636

37-
if rs.Primary.Attributes["users"] == "" {
38-
return fmt.Errorf("Users expected to not be nil")
37+
if rs.Primary.Attributes["owner"] == "" {
38+
return fmt.Errorf("owner expected to not be nil")
3939
}
4040

41-
if rs.Primary.Attributes["teams"] == "" {
42-
return fmt.Errorf("Teams expected to not be nil")
41+
if rs.Primary.Attributes["platform"] == "" {
42+
return fmt.Errorf("platform expected to not be nil")
4343
}
4444

45-
if rs.Primary.Attributes["usage"] == "" {
46-
return fmt.Errorf("Usage expected to not be nil")
45+
if rs.Primary.Attributes["app_id"] == "" {
46+
return fmt.Errorf("app_id expected to not be nil")
4747
}
4848

4949
return nil
5050
}
5151
}
5252

53-
const testAppCollaboratorConfig = `
54-
data "deploygate_app_collaborator" "current" {}
53+
const testDataSourceAppCollaboratorConfig = `
54+
data "deploygate_app_collaborator" "current" {
55+
owner = var.owner
56+
platform = var.platform
57+
app_id = var.app_id
58+
}
59+
60+
variable "platform" {
61+
type = string
62+
}
63+
64+
variable "app_id" {
65+
type = string
66+
}
67+
68+
variable "owner" {
69+
type = string
70+
}
5571
`

deploygate/provider.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,53 @@ import (
44
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
55
)
66

7-
// Provider -
7+
// Provider : terraform provider
88
func Provider() *schema.Provider {
9-
return &schema.Provider{
9+
var p *schema.Provider
10+
p = &schema.Provider{
1011
Schema: map[string]*schema.Schema{
11-
"api_token": &schema.Schema{
12+
"api_key": {
1213
Type: schema.TypeString,
1314
Optional: true,
1415
Sensitive: true,
15-
DefaultFunc: schema.EnvDefaultFunc("DG_API_TOKEN", nil),
16+
DefaultFunc: schema.EnvDefaultFunc("DG_API_KEY", nil),
1617
},
17-
"user_name": &schema.Schema{
18+
"user_name": {
1819
Type: schema.TypeString,
1920
Optional: true,
2021
DefaultFunc: schema.EnvDefaultFunc("DG_USER_NAME", nil),
2122
},
22-
"organization_name": &schema.Schema{
23+
"organization_name": {
2324
Type: schema.TypeString,
2425
Optional: true,
2526
Sensitive: true,
2627
DefaultFunc: schema.EnvDefaultFunc("DG_ORGANIZATION_NAME", nil),
2728
},
2829
},
30+
DataSourcesMap: map[string]*schema.Resource{
31+
"deploygate_app_collaborator": dataSourceAppCollaborator(),
32+
},
33+
ResourcesMap: map[string]*schema.Resource{
34+
"deploygate_app_collaborator": resourceAppCollaborator(),
35+
},
36+
}
37+
38+
p.ConfigureFunc = providerConfigure(p)
39+
return p
40+
}
41+
42+
func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
43+
return func(d *schema.ResourceData) (interface{}, error) {
44+
config := Config{
45+
apiKey: d.Get("api_key").(string),
46+
}
47+
48+
meta, err := config.Client()
49+
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return meta, nil
2955
}
3056
}

deploygate/provider_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ var testDGProviderConfigure sync.Once
2020

2121
var testDGProviders map[string]*schema.Provider
2222

23-
func testDGPreCheck(t *testing.T) {
23+
func Test_DGPreCheck(t *testing.T) {
2424
testDGProviderConfigure.Do(func() {
25-
if os.Getenv("DG_API_TOKEN") == "" || os.Getenv("DG_USER_NAME") == "" {
26-
t.Fatal("DG_API_TOKEN and DG_USER_NAME must be set for acceptance tests")
25+
if os.Getenv("DG_API_KEY") == "" {
26+
t.Fatal("DG_API_KEY must be set for acceptance tests")
2727
}
2828

29-
if os.Getenv("DG_API_TOKEN") == "" || os.Getenv("DG_ORGANIZATION_NAME") == "" {
30-
t.Fatal("DG_API_TOKEN and DG_ORGANIZATION_NAME must be set for acceptance tests")
29+
if os.Getenv("DG_USER_NAME") == "" && os.Getenv("DG_ORGANIZATION_NAME") == "" {
30+
t.Fatal("DG_USER_NAME or DG_ORGANIZATION_NAME must be set for acceptance tests")
3131
}
3232

3333
err := testDGProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))

0 commit comments

Comments
 (0)