-
Notifications
You must be signed in to change notification settings - Fork 89
/
structure_project.go
146 lines (131 loc) · 4.02 KB
/
structure_project.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
package argocd
import (
"encoding/json"
"fmt"
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Expand
func expandProject(d *schema.ResourceData) (
metadata meta.ObjectMeta,
spec application.AppProjectSpec,
err error) {
metadata = expandMetadata(d)
spec, err = expandProjectSpec(d)
return
}
func expandProjectRoles(roles []interface{}) (
projectRoles []application.ProjectRole,
err error) {
for _, _r := range roles {
r := _r.(map[string]interface{})
rolePolicies := expandStringList(r["policies"].([]interface{}))
roleGroups := expandStringList(r["groups"].([]interface{}))
projectRoles = append(
projectRoles,
application.ProjectRole{
Name: r["name"].(string),
Description: r["description"].(string),
Policies: rolePolicies,
Groups: roleGroups,
},
)
}
return
}
func expandProjectSpec(d *schema.ResourceData) (
spec application.AppProjectSpec,
err error) {
s := d.Get("spec.0").(map[string]interface{})
if v, ok := s["description"]; ok {
spec.Description = v.(string)
}
if v, ok := s["source_repos"]; ok {
for _, sr := range v.([]interface{}) {
spec.SourceRepos = append(spec.SourceRepos, sr.(string))
}
}
if v, ok := s["orphaned_resources"]; ok {
if _warn, ok := v.(map[string]interface{})["warn"]; ok {
warn := _warn.(bool)
spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{
Warn: &warn,
}
}
}
if v, ok := s["cluster_resource_whitelist"]; ok {
spec.ClusterResourceWhitelist = expandK8SGroupKind(v.(*schema.Set))
}
if v, ok := s["namespace_resource_blacklist"]; ok {
spec.NamespaceResourceBlacklist = expandK8SGroupKind(v.(*schema.Set))
}
if v, ok := s["destination"]; ok {
spec.Destinations = expandApplicationDestinations(v.(*schema.Set))
}
if v, ok := s["sync_window"]; ok {
spec.SyncWindows = expandSyncWindows(v.([]interface{}))
}
if v, ok := s["role"]; ok {
spec.Roles, err = expandProjectRoles(v.([]interface{}))
if err != nil {
return spec, err
}
for _, r := range spec.Roles {
for _, p := range r.Policies {
if err := validatePolicy(d.Get("metadata.0.name").(string), r.Name, p); err != nil {
return spec, err
}
}
}
}
return spec, nil
}
// Flatten
func flattenProject(p *application.AppProject, d *schema.ResourceData) error {
fMetadata := flattenMetadata(p.ObjectMeta, d)
fSpec := flattenProjectSpec(p.Spec)
if err := d.Set("spec", fSpec); err != nil {
e, _ := json.MarshalIndent(fSpec, "", "\t")
return fmt.Errorf("error persisting spec: %s\n%s", err, e)
}
if err := d.Set("metadata", fMetadata); err != nil {
e, _ := json.MarshalIndent(fMetadata, "", "\t")
return fmt.Errorf("error persisting metadata: %s\n%s", err, e)
}
return nil
}
func flattenProjectSpec(s application.AppProjectSpec) []map[string]interface{} {
spec := map[string]interface{}{
"cluster_resource_whitelist": flattenK8SGroupKinds(s.ClusterResourceWhitelist),
"namespace_resource_blacklist": flattenK8SGroupKinds(s.NamespaceResourceBlacklist),
"destination": flattenApplicationDestinations(s.Destinations),
"orphaned_resources": flattenProjectOrphanedResources(s.OrphanedResources),
"role": flattenProjectRoles(s.Roles),
"sync_window": flattenSyncWindows(s.SyncWindows),
"description": s.Description,
"source_repos": s.SourceRepos,
}
return []map[string]interface{}{spec}
}
func flattenProjectOrphanedResources(ors *application.OrphanedResourcesMonitorSettings) (
result map[string]bool) {
if ors != nil {
result = map[string]bool{
"warn": *ors.Warn,
}
}
return
}
func flattenProjectRoles(rs []application.ProjectRole) (
result []map[string]interface{}) {
for _, r := range rs {
result = append(result, map[string]interface{}{
"name": r.Name,
"description": r.Description,
"groups": r.Groups,
"policies": r.Policies,
})
}
return
}