-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatasource_variable.go
142 lines (120 loc) · 4.01 KB
/
datasource_variable.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
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"octopus"
)
const (
datasourceKeyVariableProjectID = "project"
datasourceKeyVariableName = "name"
datasourceKeyVariableValue = "value"
datasourceKeyVariableVariables = "variables"
datasourceKeyVariableRoles = "roles"
datasourceKeyVariableMachines = "machines"
datasourceKeyVariableActions = "actions"
)
func datasourceVariable() *schema.Resource {
return &schema.Resource{
Read: datasourceVariableRead,
Exists: datasourceVariableExists,
Schema: map[string]*schema.Schema{
datasourceKeyVariableProjectID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The variable name.",
},
datasourceKeyVariableName: &schema.Schema{
Type: schema.TypeString,
Required: true,
},
datasourceKeyVariableValue: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
datasourceKeyVariableVariables: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
datasourceKeyVariableRoles: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
datasourceKeyVariableMachines: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
datasourceKeyVariableActions: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
},
}
}
// Read a variable data-source.
func datasourceVariableRead(data *schema.ResourceData, provider interface{}) error {
projectID := data.Get(resourceKeyVariableProjectID).(string)
name := data.Get(resourceKeyVariableName).(string)
propertyHelper := propertyHelper(data)
targetScope := octopus.VariableScopes{
Environments: propertyHelper.GetStringList(resourceKeyVariableEnvironments),
Roles: propertyHelper.GetStringList(resourceKeyVariableRoles),
Machines: propertyHelper.GetStringList(resourceKeyVariableMachines),
Actions: propertyHelper.GetStringList(resourceKeyVariableActions),
}
log.Printf("Read variable '%s' in project '%s', targeting scope %+v", name, projectID, targetScope)
client := provider.(*octopus.Client)
variableSet, err := client.GetProjectVariableSet(projectID)
if err != nil {
return fmt.Errorf("Error retrieving variable set for project '%s': %s", projectID, err.Error())
}
if variableSet == nil {
return fmt.Errorf("Cannot find variable set for project '%s'", projectID)
}
matchingVariables := variableSet.GetVariablesByNameAndScopes(name, targetScope)
if len(matchingVariables) == 0 {
return fmt.Errorf("Cannot find variable '%s' in project '%s' with scope %+v", name, projectID, targetScope)
} else if len(matchingVariables) != 1 {
return fmt.Errorf("Multiple variables exactly match name '%s' in project '%s' with scope %+v", name, projectID, targetScope)
}
variable := matchingVariables[0]
data.SetId(variable.ID)
data.Set(datasourceKeyVariableName, variable.Name)
return nil
}
// Determine whether a variable data-source exists.
func datasourceVariableExists(data *schema.ResourceData, provider interface{}) (exists bool, err error) {
id := data.Id()
projectID := data.Get(resourceKeyVariableProjectID).(string)
name := data.Get(resourceKeyVariableName).(string)
log.Printf("Check if variable '%s' (name = '%s') exists in project '%s'", id, name, projectID)
client := provider.(*octopus.Client)
variableSet, err := client.GetProjectVariableSet(projectID)
if err != nil {
err = fmt.Errorf("Error retrieving variable set for project '%s': %s", projectID, err.Error())
return
}
if variableSet == nil {
err = fmt.Errorf("Cannot find variable set for project '%s'", projectID)
return
}
variable := variableSet.GetVariableByID(id)
if variable == nil {
log.Printf("Variable '%s' (name = '%s') not found", id, name)
return false, nil
}
exists = true
return
}