-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathschema_lifecycle.go
144 lines (122 loc) · 4.09 KB
/
schema_lifecycle.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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandLifecycle(d *schema.ResourceData) *octopusdeploy.Lifecycle {
name := d.Get("name").(string)
lifecycle := octopusdeploy.NewLifecycle(name)
lifecycle.ID = d.Id()
if v, ok := d.GetOk("description"); ok {
lifecycle.Description = v.(string)
}
if v, ok := d.GetOk("space_id"); ok {
lifecycle.SpaceID = v.(string)
}
releaseRetentionPolicy := expandRetentionPeriod(d, "release_retention_policy")
if releaseRetentionPolicy != nil {
lifecycle.ReleaseRetentionPolicy = *releaseRetentionPolicy
}
tentacleRetentionPolicy := expandRetentionPeriod(d, "tentacle_retention_policy")
if tentacleRetentionPolicy != nil {
lifecycle.TentacleRetentionPolicy = *tentacleRetentionPolicy
}
if attr, ok := d.GetOk("phase"); ok {
tfPhases := attr.([]interface{})
for _, tfPhase := range tfPhases {
phase := expandPhase(tfPhase.(map[string]interface{}))
lifecycle.Phases = append(lifecycle.Phases, phase)
}
}
return lifecycle
}
func flattenLifecycle(lifecycle *octopusdeploy.Lifecycle) map[string]interface{} {
if lifecycle == nil {
return nil
}
return map[string]interface{}{
"description": lifecycle.Description,
"id": lifecycle.GetID(),
"name": lifecycle.Name,
"phase": flattenPhases(lifecycle.Phases),
"space_id": lifecycle.SpaceID,
"release_retention_policy": flattenRetentionPeriod(lifecycle.ReleaseRetentionPolicy),
"tentacle_retention_policy": flattenRetentionPeriod(lifecycle.TentacleRetentionPolicy),
}
}
func getLifecycleDataSchema() map[string]*schema.Schema {
dataSchema := getLifecycleSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"ids": getQueryIDs(),
"lifecycles": {
Computed: true,
Description: "A list of lifecycles that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
"partial_name": getQueryPartialName(),
"skip": getQuerySkip(),
"take": getQueryTake(),
}
}
func getLifecycleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": getDescriptionSchema(),
"id": getIDSchema(),
"name": getNameSchema(true),
"phase": {
Computed: true,
Elem: &schema.Resource{Schema: getPhaseSchema()},
Optional: true,
Type: schema.TypeList,
},
"release_retention_policy": {
Computed: true,
DefaultFunc: func() (interface{}, error) {
return flattenRetentionPeriod(octopusdeploy.RetentionPeriod{
Unit: "Days",
QuantityToKeep: 30,
}), nil
},
Elem: &schema.Resource{Schema: getRetentionPeriodSchema()},
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
"space_id": getSpaceIDSchema(),
"tentacle_retention_policy": {
Computed: true,
DefaultFunc: func() (interface{}, error) {
return flattenRetentionPeriod(octopusdeploy.RetentionPeriod{
Unit: "Days",
QuantityToKeep: 30,
}), nil
},
Elem: &schema.Resource{Schema: getRetentionPeriodSchema()},
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
}
}
func setLifecycle(ctx context.Context, d *schema.ResourceData, lifecycle *octopusdeploy.Lifecycle) error {
d.Set("description", lifecycle.Description)
d.Set("id", lifecycle.GetID())
d.Set("name", lifecycle.Name)
d.Set("space_id", lifecycle.SpaceID)
if err := d.Set("phase", flattenPhases(lifecycle.Phases)); err != nil {
return fmt.Errorf("error setting phase: %s", err)
}
if err := d.Set("release_retention_policy", flattenRetentionPeriod(lifecycle.ReleaseRetentionPolicy)); err != nil {
return fmt.Errorf("error setting release_retention_policy: %s", err)
}
if err := d.Set("tentacle_retention_policy", flattenRetentionPeriod(lifecycle.TentacleRetentionPolicy)); err != nil {
return fmt.Errorf("error setting tentacle_retention_policy: %s", err)
}
d.SetId(lifecycle.GetID())
return nil
}