-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-profile.go
206 lines (168 loc) · 6.16 KB
/
docker-profile.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package resources
import (
"context"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
dockerprofile "github.com/CheckPointSW/terraform-provider-infinity-next/internal/resources/docker-profile"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceDockerProfile() *schema.Resource {
return &schema.Resource{
Description: "Docker profile",
CreateContext: resourceDockerProfileCreate,
ReadContext: resourceDockerProfileRead,
UpdateContext: resourceDockerProfileUpdate,
DeleteContext: resourceDockerProfileDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("additional_settings") {
return diff.SetNewComputed("additional_settings_ids")
}
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"name": {
Type: schema.TypeString,
Description: "The name of the resource, also acts as its unique ID",
Required: true,
},
"profile_type": {
Type: schema.TypeString,
Computed: true,
},
"additional_settings": {
Type: schema.TypeMap,
Description: "Controls the settings of the connected agents",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"additional_settings_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"defined_applications_only": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"max_number_of_agents": {
Type: schema.TypeInt,
Description: "Sets the maximum number of agents that can be connected to this profile",
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtMost(1000)),
},
"authentication_token": {
Type: schema.TypeString,
Description: "The token used to register an agent to the profile",
Computed: true,
},
},
}
}
func resourceDockerProfileCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
createInput, err := dockerprofile.CreateDockerProfileInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform DockerProfile Create", err, diags)
}
profile, err := dockerprofile.NewDockerProfile(c, createInput)
if err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform DockerProfile Create", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following DockerProfile Create", err, diags)
}
if err = dockerprofile.ReadDockerProfileToResourceData(profile, d); err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to read DockerProfile to resource data", err, diags)
}
return diags
}
func resourceDockerProfileRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
id := d.Id()
profile, err := dockerprofile.GetDockerProfile(c, id)
if err != nil {
return utils.DiagError("unable to perform DockerProfile Read", err, diags)
}
if err := dockerprofile.ReadDockerProfileToResourceData(profile, d); err != nil {
return utils.DiagError("unable to perform DockerProfile Read", err, diags)
}
return diags
}
func resourceDockerProfileUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
updateInput, err := dockerprofile.UpdateDockerProfileInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform DockerProfile Update", err, diags)
}
result, err := dockerprofile.UpdateDockerProfile(c, d.Id(), updateInput)
if err != nil || !result {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform DockerProfile Update", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following DockerProfile Update", err, diags)
}
profile, err := dockerprofile.GetDockerProfile(c, d.Id())
if err != nil {
return utils.DiagError("failed get DockerProfile after update", err, diags)
}
if err := dockerprofile.ReadDockerProfileToResourceData(profile, d); err != nil {
return utils.DiagError("unable to perform read DockerProfile read after update", err, diags)
}
return diags
}
func resourceDockerProfileDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
ID := d.Id()
result, err := dockerprofile.DeleteDockerProfile(c, ID)
if err != nil || !result {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform DockerProfile Delete", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following DockerProfile Delete", err, diags)
}
d.SetId("")
return diags
}