From 24123defef42852a286da257917135239516be9f Mon Sep 17 00:00:00 2001 From: sternik <147771+sternik@users.noreply.github.com> Date: Tue, 17 Sep 2024 13:32:35 +0200 Subject: [PATCH] bugfix: editing customer variables (#73) --- docs/resources/template.md | 2 +- .../imagefactory_template/resource.tf | 2 +- imagefactory/variable/resource.go | 4 +- pkg/graphql/graphql.go | 74 +++++++++++++++++++ pkg/graphql/schema.graphql | 14 +++- pkg/graphql/variable.graphql | 9 ++- pkg/sdk/api.go | 2 +- pkg/sdk/client.go | 16 +++- pkg/sdk/model.go | 4 +- 9 files changed, 116 insertions(+), 11 deletions(-) diff --git a/docs/resources/template.md b/docs/resources/template.md index e715526..7131321 100644 --- a/docs/resources/template.md +++ b/docs/resources/template.md @@ -162,7 +162,7 @@ resource "imagefactory_template" "azure_template" { additional_signatures { variable_name = imagefactory_variable.uefi_key.name # If the variable is not defined in the template, the value can be set directly - # variable_value = "UEFI_KEY" + # variable_name = "UEFI_KEY" } } } diff --git a/examples/resources/imagefactory_template/resource.tf b/examples/resources/imagefactory_template/resource.tf index bed48f5..b14229d 100644 --- a/examples/resources/imagefactory_template/resource.tf +++ b/examples/resources/imagefactory_template/resource.tf @@ -147,7 +147,7 @@ resource "imagefactory_template" "azure_template" { additional_signatures { variable_name = imagefactory_variable.uefi_key.name # If the variable is not defined in the template, the value can be set directly - # variable_value = "UEFI_KEY" + # variable_name = "UEFI_KEY" } } } diff --git a/imagefactory/variable/resource.go b/imagefactory/variable/resource.go index fb6964f..71e951b 100644 --- a/imagefactory/variable/resource.go +++ b/imagefactory/variable/resource.go @@ -1,4 +1,4 @@ -// Copyright 2022 Nordcloud Oy or its affiliates. All Rights Reserved. +// Copyright 2022-2024 Nordcloud Oy or its affiliates. All Rights Reserved. package variable @@ -79,7 +79,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } } - input := sdk.NewVariable{ + input := sdk.VariableChanges{ Name: graphql.String(newVariableName), Value: graphql.String(variableValue), } diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 80f6d6c..9d82562 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -2889,6 +2889,74 @@ func (client *Client) CreateVariable(vars *CreateVariableVariables) (*CreateVari return CreateVariable(client.Url, client.Client, vars) } +// +// mutation UpdateVariable($input: VariableChanges!) +// + +type UpdateVariableVariables struct { + Input VariableChanges `json:"input"` +} + +type UpdateVariableResponse struct { + UpdateVariable struct { + Name string `json:"name"` + Hash string `json:"hash"` + } `json:"updateVariable"` +} + +type UpdateVariableRequest struct { + *http.Request +} + +func NewUpdateVariableRequest(url string, vars *UpdateVariableVariables) (*UpdateVariableRequest, error) { + variables, err := json.Marshal(vars) + if err != nil { + return nil, err + } + b, err := json.Marshal(&GraphQLOperation{ + Variables: variables, + Query: `mutation UpdateVariable($input: VariableChanges!) { + updateVariable(input: $input) { + name + hash + } +}`, + }) + if err != nil { + return nil, err + } + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + return &UpdateVariableRequest{req}, nil +} + +func (req *UpdateVariableRequest) Execute(client *http.Client) (*UpdateVariableResponse, error) { + resp, err := execute(client, req.Request) + if err != nil { + return nil, err + } + var result UpdateVariableResponse + if err := json.Unmarshal(resp.Data, &result); err != nil { + return nil, err + } + return &result, nil +} + +func UpdateVariable(url string, client *http.Client, vars *UpdateVariableVariables) (*UpdateVariableResponse, error) { + req, err := NewUpdateVariableRequest(url, vars) + if err != nil { + return nil, err + } + return req.Execute(client) +} + +func (client *Client) UpdateVariable(vars *UpdateVariableVariables) (*UpdateVariableResponse, error) { + return UpdateVariable(client.Url, client.Client, vars) +} + // // mutation DeleteVariable($input: CustomerVariableNameInput!) // @@ -3808,6 +3876,11 @@ type TemplatesSort struct { Order SortOrder `json:"order"` } +type VariableChanges struct { + Name String `json:"name"` + Value String `json:"value"` +} + // // Objects // @@ -4137,6 +4210,7 @@ type Mutation struct { UpdateRole Role `json:"updateRole"` UpdateRoleBinding RoleBinding `json:"updateRoleBinding"` UpdateTemplate Template `json:"updateTemplate"` + UpdateVariable Variable `json:"updateVariable"` } type Notification struct { diff --git a/pkg/graphql/schema.graphql b/pkg/graphql/schema.graphql index d309959..5c83347 100644 --- a/pkg/graphql/schema.graphql +++ b/pkg/graphql/schema.graphql @@ -1297,7 +1297,7 @@ input TemplateChanges { } -# Copyright 2021-2022 Nordcloud Oy or its affiliates. All Rights Reserved. +# Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved. type Variable { name: String! @@ -1313,6 +1313,11 @@ input NewVariable { value: String! } +input VariableChanges { + name: String! + value: String! +} + # Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved. @@ -1958,7 +1963,7 @@ extend type Query { } -# Copyright 2021-2022 Nordcloud Oy or its affiliates. All Rights Reserved. +# Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved. input CustomerVariableNameInput { variableName: String! @@ -1970,6 +1975,11 @@ extend type Mutation { """ createVariable(input: NewVariable!): Variable! + """ + updateVariable updates existing customer variable. + """ + updateVariable(input: VariableChanges!): Variable! + """ deleteVariable removes existing customer variable. """ diff --git a/pkg/graphql/variable.graphql b/pkg/graphql/variable.graphql index 4460928..33e560a 100644 --- a/pkg/graphql/variable.graphql +++ b/pkg/graphql/variable.graphql @@ -1,4 +1,4 @@ -# Copyright 2022 Nordcloud Oy or its affiliates. All Rights Reserved. +# Copyright 2022-2024 Nordcloud Oy or its affiliates. All Rights Reserved. query GetVariables { variables { @@ -20,6 +20,13 @@ mutation CreateVariable($input: NewVariable!) { } } +mutation UpdateVariable($input: VariableChanges!) { + updateVariable(input: $input) { + name + hash + } +} + mutation DeleteVariable($input: CustomerVariableNameInput!) { deleteVariable(input: $input) } diff --git a/pkg/sdk/api.go b/pkg/sdk/api.go index 627b749..df635c1 100644 --- a/pkg/sdk/api.go +++ b/pkg/sdk/api.go @@ -55,7 +55,7 @@ type API interface { // Variables CRUD GetVariable(name string) (Variable, error) CreateVariable(input NewVariable) (Variable, error) - UpdateVariable(input NewVariable) (Variable, error) + UpdateVariable(input VariableChanges) (Variable, error) DeleteVariable(name string) error // Actions diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index 51adc87..3d35a83 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -668,8 +668,20 @@ func (c APIClient) CreateVariable(input NewVariable) (Variable, error) { return Variable(r.CreateVariable), nil } -func (c APIClient) UpdateVariable(input NewVariable) (Variable, error) { - return c.CreateVariable(input) +func (c APIClient) UpdateVariable(input VariableChanges) (Variable, error) { + req, err := graphql.NewUpdateVariableRequest(c.apiURL, &graphql.UpdateVariableVariables{ + Input: graphql.VariableChanges(input), + }) + if err != nil { + return Variable{}, fmt.Errorf("getting update variable request %w", err) + } + + r := &graphql.Mutation{} + if err := c.graphqlAPI.Execute(req.Request, r); err != nil { + return Variable{}, fmt.Errorf("updating variable %w", err) + } + + return Variable(r.UpdateVariable), nil } func (c APIClient) DeleteVariable(name string) error { diff --git a/pkg/sdk/model.go b/pkg/sdk/model.go index 58c0444..fcb417f 100644 --- a/pkg/sdk/model.go +++ b/pkg/sdk/model.go @@ -1,4 +1,4 @@ -// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved. +// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved. package sdk @@ -49,3 +49,5 @@ type APIKeyChanges graphql.ApiKeyChanges type Variable graphql.Variable type NewVariable graphql.NewVariable + +type VariableChanges graphql.VariableChanges