-
Notifications
You must be signed in to change notification settings - Fork 8
/
resource_project_git_details.go
152 lines (119 loc) · 3.73 KB
/
resource_project_git_details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package looker
import (
"strings"
"github.com/billtrust/looker-go-sdk/client/project"
"github.com/billtrust/looker-go-sdk/models"
"github.com/go-openapi/strfmt"
apiclient "github.com/billtrust/looker-go-sdk/client"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceProjectGitDetails() *schema.Resource {
return &schema.Resource{
Create: resourceProjectGitDetailsCreate,
Read: resourceProjectGitDetailsRead,
Delete: resourceProjectGitDetailsDelete,
Update: resourceProjectGitDetailsDelete,
Exists: resourceProjectGitDetailsExists,
Importer: &schema.ResourceImporter{
State: resourceProjectGitDetailsImport,
},
Schema: map[string]*schema.Schema{
"project_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"git_remote_url": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func setProjectGitDetails(d *schema.ResourceData, m interface{}) error {
client := m.(*apiclient.LookerAPI30Reference)
err := updateSession(client, "dev")
if err != nil {
return err
}
gitRemoteURL := strfmt.URI(d.Get("git_remote_url").(string))
projectID := d.Get("project_id").(string)
params := project.NewUpdateProjectParams()
params.ProjectID = projectID
params.Body = &models.Project{}
params.Body.GitRemoteURL = &gitRemoteURL
_, err = client.Project.UpdateProject(params)
if err != nil {
return err
}
return nil
}
func resourceProjectGitDetailsCreate(d *schema.ResourceData, m interface{}) error {
err := setProjectGitDetails(d, m)
if err != nil {
if strings.Contains(err.Error(), "Not found") {
d.SetId("")
return nil
}
return err
}
d.SetId(d.Get("project_id").(string))
return resourceProjectGitDetailsRead(d, m)
}
func resourceProjectGitDetailsRead(d *schema.ResourceData, m interface{}) error {
client := m.(*apiclient.LookerAPI30Reference)
err := updateSession(client, "dev")
if err != nil {
return err
}
projectID := d.Id()
params := project.NewProjectParams()
params.ProjectID = projectID
result, err := client.Project.Project(params)
if err != nil {
if strings.Contains(err.Error(), "Not found") {
d.SetId("")
return nil
}
return err
}
d.Set("project_id", result.Payload.ID)
d.Set("git_remote_url", result.Payload.GitRemoteURL.String())
return nil
}
func resourceProjectGitDetailsUpdate(d *schema.ResourceData, m interface{}) error {
err := setProjectGitDetails(d, m)
if err != nil {
return err
}
return resourceProjectGitDetailsRead(d, m)
}
func resourceProjectGitDetailsDelete(d *schema.ResourceData, m interface{}) error {
// TODO: Deleting this resource should set the git fields back to blank values. not implementing this yet since leaving the values does not have any negative effect
return nil
}
func resourceProjectGitDetailsExists(d *schema.ResourceData, m interface{}) (b bool, e error) {
// Exists - This is called to verify a resource still exists. It is called prior to Read,
// and lowers the burden of Read to be able to assume the resource exists.
client := m.(*apiclient.LookerAPI30Reference)
// TODO Not sure if we should always set session to "dev" instead of "production" when checking if it exists? will dev always show all dev+prod projects?
err := updateSession(client, "dev")
if err != nil {
return false, err
}
params := project.NewProjectParams()
params.ProjectID = d.Id()
_, err = client.Project.Project(params)
if err != nil {
if strings.Contains(err.Error(), "Not found") {
return false, nil
}
return false, err
}
return true, nil
}
func resourceProjectGitDetailsImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
if err := resourceProjectGitDetailsRead(d, m); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}