/
resource_artifact_dependency.go
182 lines (159 loc) · 4.94 KB
/
resource_artifact_dependency.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package teamcity
import (
"fmt"
api "github.com/cvbarros/go-teamcity/teamcity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceArtifactDependency() *schema.Resource {
return &schema.Resource{
Create: resourceArtifactDependencyCreate,
Read: resourceArtifactDependencyRead,
Delete: resourceArtifactDependencyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"build_config_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"source_build_config_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"dependency_revision": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: string(api.LatestSuccessfulBuild),
ValidateFunc: validation.StringInSlice([]string{
string(api.LatestFinishedBuild),
string(api.LastBuildFinishedWithTag),
string(api.LatestPinnedBuild),
string(api.LatestSuccessfulBuild),
string(api.BuildWithSpecifiedNumber),
string(api.BuildFromSameChain),
}, false),
},
"path_rules": {
Type: schema.TypeList,
ForceNew: true,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"revision": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"clean_destination": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},
},
}
}
func resourceArtifactDependencyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
var buildConfigID string
if v, ok := d.GetOk("build_config_id"); ok {
buildConfigID = v.(string)
}
// validates the Build Configuration exists
if _, err := client.BuildTypes.GetByID(buildConfigID); err != nil {
return fmt.Errorf("invalid build_config_id '%s' - Build configuration does not exist", buildConfigID)
}
depService := client.DependencyService(buildConfigID)
opt, err := expandArtifactDependencyOptions(d)
if err != nil {
return err
}
dep, err := api.NewArtifactDependency(d.Get("source_build_config_id").(string), opt)
if err != nil {
return err
}
out, err := depService.AddArtifactDependency(dep)
if err != nil {
return err
}
d.SetId(out.ID())
return resourceArtifactDependencyRead(d, meta)
}
func resourceArtifactDependencyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client).DependencyService(d.Get("build_config_id").(string))
dt, err := getArtifactDependency(client, d.Id())
if err != nil {
return err
}
if err := d.Set("build_config_id", dt.BuildTypeID()); err != nil {
return err
}
if dt.Options.CleanDestination {
if err := d.Set("clean_destination", dt.Options.CleanDestination); err != nil {
return err
}
}
if err := d.Set("dependency_revision", string(dt.Options.ArtifactRevisionType)); err != nil {
return err
}
if err := d.Set("path_rules", flattenStringSlice(dt.Options.PathRules)); err != nil {
return err
}
if dt.Options.ArtifactRevisionType == api.BuildWithSpecifiedNumber || dt.Options.ArtifactRevisionType == api.LastBuildFinishedWithTag {
if err := d.Set("revision", dt.Options.RevisionNumber); err != nil {
return err
}
} else {
d.Set("revision", nil)
}
return d.Set("source_build_config_id", dt.SourceBuildTypeID)
}
func resourceArtifactDependencyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
dep := client.DependencyService(d.Get("build_config_id").(string))
return dep.DeleteArtifact(d.Id())
}
func getArtifactDependency(c *api.DependencyService, id string) (*api.ArtifactDependency, error) {
dt, err := c.GetArtifactByID(id)
if err != nil {
return nil, err
}
return dt, nil
}
func expandArtifactDependencyOptions(d *schema.ResourceData) (*api.ArtifactDependencyOptions, error) {
var cleanDestination bool
var pathRules []string
var revision string
var revisionType api.ArtifactDependencyRevision
if v, ok := d.GetOkExists("clean_destination"); ok {
cleanDestination = v.(bool)
}
if v, ok := d.GetOk("path_rules"); ok {
pathRules = expandStringSlice(v.([]interface{}))
}
if v, ok := d.GetOk("dependency_revision"); ok {
revisionType = api.ArtifactDependencyRevision(v.(string))
}
if revisionType == api.LastBuildFinishedWithTag || revisionType == api.BuildWithSpecifiedNumber {
if v, ok := d.GetOk("revision"); ok {
revision = v.(string)
} else {
return nil, fmt.Errorf("'revision' property is required if using '%s' or '%s' for 'dependency_revision'", api.LastBuildFinishedWithTag, api.BuildWithSpecifiedNumber)
}
} else {
//Ignore revison and remove from config/state if not used
if err := d.Set("revision", nil); err != nil {
return nil, err
}
}
out, err := api.NewArtifactDependencyOptions(pathRules, revisionType, cleanDestination, revision)
if err != nil {
return nil, err
}
return out, nil
}