-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathschema_package_reference.go
122 lines (103 loc) · 3.66 KB
/
schema_package_reference.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
package octopusdeploy
import (
"strconv"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func addPrimaryPackageSchema(element *schema.Resource, required bool) error {
if element == nil {
return createInvalidParameterError("addPrimaryPackageSchema", "element")
}
element.Schema["primary_package"] = getPackageSchema(required)
element.Schema["primary_package"].MaxItems = 1
// TODO: update name in schema to always be empty string
return nil
}
func addPackagesSchema(element *schema.Resource, primaryIsRequired bool) {
addPrimaryPackageSchema(element, primaryIsRequired)
element.Schema["package"] = getPackageSchema(false)
packageElementSchema := element.Schema["package"].Elem.(*schema.Resource).Schema
packageElementSchema["name"] = &schema.Schema{
Description: "The name of the package",
Required: true,
Type: schema.TypeString,
}
packageElementSchema["extract_during_deployment"] = &schema.Schema{
Computed: true,
Description: "Whether to extract the package during deployment",
Optional: true,
Type: schema.TypeBool,
}
}
func flattenPackageReference(packageReference octopusdeploy.PackageReference) map[string]interface{} {
flattenedPackageReference := map[string]interface{}{
"acquisition_location": packageReference.AcquisitionLocation,
"feed_id": packageReference.FeedID,
"id": packageReference.ID,
"name": packageReference.Name,
"package_id": packageReference.PackageID,
"properties": packageReference.Properties,
}
if v, ok := packageReference.Properties["Extract"]; ok {
flattenedPackageReference["extract_during_deployment"] = v
}
return flattenedPackageReference
}
func getPackageSchema(required bool) *schema.Schema {
return &schema.Schema{
Computed: !required,
Description: "The package assocated with this action.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"acquisition_location": {
Default: "Server",
Description: "Whether to acquire this package on the server ('Server'), target ('ExecutionTarget') or not at all ('NotAcquired'). Can be an expression",
Optional: true,
Type: schema.TypeString,
},
"feed_id": {
Default: "feeds-builtin",
Description: "The feed ID associated with this package reference.",
Optional: true,
Type: schema.TypeString,
},
"id": getIDSchema(),
"name": getNameSchema(false),
"package_id": {
Description: "The ID of the package.",
Required: true,
Type: schema.TypeString,
},
"properties": {
Computed: true,
Description: "A list of properties associated with this package.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeMap,
},
},
},
Optional: !required,
Required: required,
Type: schema.TypeList,
}
}
func expandPackageReference(tfPkg map[string]interface{}) octopusdeploy.PackageReference {
pkg := octopusdeploy.PackageReference{
AcquisitionLocation: tfPkg["acquisition_location"].(string),
FeedID: tfPkg["feed_id"].(string),
Name: getStringOrEmpty(tfPkg["name"]),
PackageID: tfPkg["package_id"].(string),
Properties: map[string]string{},
}
if v, ok := tfPkg["extract_during_deployment"]; ok {
pkg.Properties["Extract"] = strconv.FormatBool(v.(bool))
}
if properties := tfPkg["properties"]; properties != nil {
propertyMap := properties.(map[string]interface{})
for k, v := range propertyMap {
pkg.Properties[k] = v.(string)
}
}
return pkg
}