-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_application.go
143 lines (116 loc) · 3.51 KB
/
resource_application.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
package main
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/pactflow/terraform/broker"
"github.com/pactflow/terraform/client"
)
func application() *schema.Resource {
return &schema.Resource{
Create: applicationCreate,
Update: applicationUpdate,
Read: applicationRead,
Delete: applicationDelete,
Importer: &schema.ResourceImporter{State: schema.ImportStatePassthrough},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the Pacticipant",
},
"repository_url": {
Type: schema.TypeString,
Optional: true,
Description: "URL or location of the VCS repository",
},
"main_branch": {
Type: schema.TypeString,
Optional: true,
Description: "Main (default) branch",
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Description: "The display name of the pacticipant",
},
},
}
}
func applicationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
name := d.Get("name").(string)
url := d.Get("repository_url").(string)
branch := d.Get("main_branch").(string)
displayName := d.Get("display_name").(string)
log.Println("[DEBUG] creating pacticipant", name)
pacticipant := broker.Pacticipant{
Name: name,
RepositoryURL: url,
MainBranch: branch,
DisplayName: displayName,
}
_, err := client.CreatePacticipant(pacticipant)
if err != nil {
return fmt.Errorf("error creating application: %w", err)
}
d.SetId(name)
d.Set("name", pacticipant.Name)
d.Set("repository_url", pacticipant.RepositoryURL)
d.Set("main_branch", pacticipant.MainBranch)
d.Set("display_name", pacticipant.DisplayName)
return nil
}
func applicationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
name := d.Get("name").(string)
url := d.Get("repository_url").(string)
branch := d.Get("main_branch").(string)
displayName := d.Get("display_name").(string)
log.Println("[DEBUG] updating pacticipant", name)
pacticipant := broker.Pacticipant{
Name: name,
RepositoryURL: url,
MainBranch: branch,
DisplayName: displayName,
}
_, err := client.UpdatePacticipant(pacticipant)
if err != nil {
return fmt.Errorf("error updating application: %w", err)
}
d.SetId(name)
d.Set("name", pacticipant.Name)
d.Set("repository_url", pacticipant.RepositoryURL)
d.Set("main_branch", pacticipant.MainBranch)
d.Set("display_name", pacticipant.DisplayName)
return nil
}
func applicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
log.Println("[DEBUG] reading pacticipant", d.Id())
pacticipant, err := client.ReadPacticipant(d.Id())
log.Println("[DEBUG] have pacticipant for READ", pacticipant)
if err != nil {
return fmt.Errorf("error reading application: %w", err)
}
d.SetId(pacticipant.Name)
d.Set("name", pacticipant.Name)
d.Set("repository_url", pacticipant.RepositoryURL)
d.Set("main_branch", pacticipant.MainBranch)
d.Set("display_name", pacticipant.DisplayName)
return nil
}
func applicationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
name := d.Get("name").(string)
log.Println("[DEBUG] deleting pacticipant", name)
err := client.DeletePacticipant(broker.Pacticipant{
Name: name,
})
if err != nil {
d.SetId("")
return fmt.Errorf("error deleting application: %w", err)
}
return nil
}