-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.go
74 lines (62 loc) · 2.17 KB
/
create.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
package kubernetesprofile
import (
"context"
"fmt"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
models "github.com/CheckPointSW/terraform-provider-infinity-next/internal/models/kubernetes-profile"
)
func CreateKubernetesProfileInputFromResourceData(d *schema.ResourceData) (models.CreateKubernetesProfileInput, error) {
var res models.CreateKubernetesProfileInput
res.Name = d.Get("name").(string)
res.ProfileSubType = d.Get("profile_sub_type").(string)
res.OnlyDefinedApplications = d.Get("defined_applications_only").(bool)
res.Authentication.MaxNumberOfAgents = d.Get("max_number_of_agents").(int)
res.AdditionalSettings = mapToKeyValueInput(d, "additional_settings")
return res, nil
}
func mapToKeyValueInput(d *schema.ResourceData, key string) []models.KeyValueInput {
mapUserInput := d.Get(key).(map[string]any)
res := make([]models.KeyValueInput, 0, len(mapUserInput))
for key, val := range mapUserInput {
res = append(res,
models.KeyValueInput{
Key: key,
Value: val.(string),
})
}
return res
}
func NewKubernetesProfile(ctx context.Context, c *api.Client, input models.CreateKubernetesProfileInput) (models.KubernetesProfile, error) {
vars := map[string]any{"profileInput": input}
res, err := c.MakeGraphQLRequest(ctx, `
mutation newKubernetesProfile($profileInput: KubernetesProfileInput)
{
newKubernetesProfile (profileInput: $profileInput) {
id
name
profileType
profileSubType
authentication {
token
maxNumberOfAgents
}
additionalSettings {
id
key
value
}
onlyDefinedApplications
}
}
`, "newKubernetesProfile", vars)
if err != nil {
return models.KubernetesProfile{}, fmt.Errorf("failed to create new KubernetesProfile: %w", err)
}
profile, err := utils.UnmarshalAs[models.KubernetesProfile](res)
if err != nil {
return models.KubernetesProfile{}, fmt.Errorf("failed to convert response to KubernetesProfile struct. Error: %w", err)
}
return profile, nil
}