-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathschema_channel.go
133 lines (113 loc) · 3.51 KB
/
schema_channel.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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandChannel(d *schema.ResourceData) *octopusdeploy.Channel {
name := d.Get("name").(string)
projectID := d.Get("project_id").(string)
channel := octopusdeploy.NewChannel(name, projectID)
channel.ID = d.Id()
if v, ok := d.GetOk("description"); ok {
channel.Description = v.(string)
}
if v, ok := d.GetOk("is_default"); ok {
channel.IsDefault = v.(bool)
}
if v, ok := d.GetOk("lifecycle_id"); ok {
channel.LifecycleID = v.(string)
}
if v, ok := d.GetOk("space_id"); ok {
channel.SpaceID = v.(string)
}
if v, ok := d.GetOk("tenant_tags"); ok {
channel.TenantTags = getSliceFromTerraformTypeList(v)
}
if v, ok := d.GetOk("rule"); ok {
channelRules := v.([]interface{})
for _, channelRule := range channelRules {
rule := expandChannelRule(channelRule.(map[string]interface{}))
channel.Rules = append(channel.Rules, rule)
}
}
return channel
}
func flattenChannel(channel *octopusdeploy.Channel) map[string]interface{} {
if channel == nil {
return nil
}
return map[string]interface{}{
"description": channel.Description,
"is_default": channel.IsDefault,
"lifecycle_id": channel.LifecycleID,
"name": channel.Name,
"project_id": channel.ProjectID,
"rule": flattenChannelRules(channel.Rules),
"space_id": channel.SpaceID,
"tenant_tags": channel.TenantTags,
}
}
func getChannelDataSchema() map[string]*schema.Schema {
dataSchema := getChannelSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"channels": {
Computed: true,
Description: "A channel that matches the specified filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
"ids": getQueryIDs(),
"partial_name": getQueryPartialName(),
"skip": getQuerySkip(),
"take": getQueryTake(),
}
}
func getChannelSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": getDescriptionSchema(),
"id": getIDSchema(),
"is_default": {
Description: "Indicates if this is the default channel for the associated project.",
Optional: true,
Type: schema.TypeBool,
},
"lifecycle_id": {
Description: "The lifecycle ID associated with this channel.",
Optional: true,
Type: schema.TypeString,
},
"name": getNameSchema(true),
"project_id": {
Description: "The project ID associated with this channel.",
Required: true,
Type: schema.TypeString,
},
"rule": {
Description: "A list of rules associated with this channel.",
Elem: &schema.Resource{Schema: getChannelRuleSchema()},
Optional: true,
Type: schema.TypeList,
},
"space_id": getSpaceIDSchema(),
"tenant_tags": getTenantTagsSchema(),
}
}
func setChannel(ctx context.Context, d *schema.ResourceData, channel *octopusdeploy.Channel) error {
d.Set("description", channel.Description)
d.Set("is_default", channel.IsDefault)
d.Set("lifecycle_id", channel.LifecycleID)
d.Set("name", channel.Name)
d.Set("project_id", channel.ProjectID)
d.Set("space_id", channel.SpaceID)
if err := d.Set("rule", flattenChannelRules(channel.Rules)); err != nil {
return fmt.Errorf("error setting rule: %s", err)
}
if err := d.Set("tenant_tags", channel.TenantTags); err != nil {
return fmt.Errorf("error setting tenant_tags: %s", err)
}
return nil
}