Skip to content

Commit

Permalink
Refactoring: split update_opts.go and updateopts_test.go
Browse files Browse the repository at this point in the history
update_opts.go contains helpers for Ironic PATCH API that are not tied
to the provisioner and thus are better haused in the clients module
(renamed to match the existing convention).

updateopts_test.go is a mix of tests for update_opts.go and a few
provision-specific functions. The former are migrated to clients as
well, the latter - to the more suitable provision_test.py.

Signed-off-by: Dmitry Tantsur <dtantsur@protonmail.com>
  • Loading branch information
dtantsur committed May 14, 2024
1 parent b87793e commit 6d8ab3b
Show file tree
Hide file tree
Showing 7 changed files with 1,264 additions and 1,266 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ironic
package clients

import (
"fmt"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/gophercloud/gophercloud/v2/openstack/baremetal/v1/nodes"
)

type optionsData map[string]interface{}
type UpdateOptsData map[string]interface{}

func optionValueEqual(current, value interface{}) bool {
if reflect.DeepEqual(current, value) {
Expand Down Expand Up @@ -125,30 +125,30 @@ func getUpdateOperation(name string, currentData map[string]interface{}, desired
return nil
}

type nodeUpdater struct {
type NodeUpdater struct {
Updates nodes.UpdateOpts
log logr.Logger
}

func updateOptsBuilder(logger logr.Logger) *nodeUpdater {
return &nodeUpdater{
func UpdateOptsBuilder(logger logr.Logger) *NodeUpdater {
return &NodeUpdater{
log: logger,
}
}

func (nu *nodeUpdater) logger(basepath, option string) logr.Logger {
func (nu *NodeUpdater) logger(basepath, option string) logr.Logger {
log := nu.log.WithValues("option", option)
if basepath != "" {
log = log.WithValues("section", basepath[1:])
}
return log
}

func (nu *nodeUpdater) path(basepath, option string) string {
func (nu *NodeUpdater) path(basepath, option string) string {
return fmt.Sprintf("%s/%s", basepath, option)
}

func (nu *nodeUpdater) setSectionUpdateOpts(currentData map[string]interface{}, settings optionsData, basepath string) {
func (nu *NodeUpdater) setSectionUpdateOpts(currentData map[string]interface{}, settings UpdateOptsData, basepath string) {
for name, desiredValue := range settings {
updateOp := getUpdateOperation(name, currentData, desiredValue,
nu.path(basepath, name), nu.logger(basepath, name))
Expand All @@ -158,25 +158,25 @@ func (nu *nodeUpdater) setSectionUpdateOpts(currentData map[string]interface{},
}
}

func (nu *nodeUpdater) SetTopLevelOpt(name string, desiredValue, currentValue interface{}) *nodeUpdater {
func (nu *NodeUpdater) SetTopLevelOpt(name string, desiredValue, currentValue interface{}) *NodeUpdater {
currentData := map[string]interface{}{name: currentValue}
desiredData := optionsData{name: desiredValue}
desiredData := UpdateOptsData{name: desiredValue}

nu.setSectionUpdateOpts(currentData, desiredData, "")
return nu
}

func (nu *nodeUpdater) SetPropertiesOpts(settings optionsData, node *nodes.Node) *nodeUpdater {
func (nu *NodeUpdater) SetPropertiesOpts(settings UpdateOptsData, node *nodes.Node) *NodeUpdater {
nu.setSectionUpdateOpts(node.Properties, settings, "/properties")
return nu
}

func (nu *nodeUpdater) SetInstanceInfoOpts(settings optionsData, node *nodes.Node) *nodeUpdater {
func (nu *NodeUpdater) SetInstanceInfoOpts(settings UpdateOptsData, node *nodes.Node) *NodeUpdater {
nu.setSectionUpdateOpts(node.InstanceInfo, settings, "/instance_info")
return nu
}

func (nu *nodeUpdater) SetDriverInfoOpts(settings optionsData, node *nodes.Node) *nodeUpdater {
func (nu *NodeUpdater) SetDriverInfoOpts(settings UpdateOptsData, node *nodes.Node) *NodeUpdater {
nu.setSectionUpdateOpts(node.DriverInfo, settings, "/driver_info")
return nu
}
Loading

0 comments on commit 6d8ab3b

Please sign in to comment.