Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made provider block optional, ignored hosts labels #184

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
9 changes: 1 addition & 8 deletions internal/acceptance_test/resource_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@ import (
)

func metalSSHKeyConfigBasic(name, publicSSHKey string) string {
return fmt.Sprintf(`
provider "hpegl" {
metal {
}
alias = "test"
}

return fmt.Sprintf(`
resource "hpegl_metal_ssh_key" "test" {
provider = hpegl.test
name = %q
public_key = %q
}
Expand Down
14 changes: 10 additions & 4 deletions internal/resources/resource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func hostSchema() map[string]*schema.Schema {
Type: schema.TypeMap,
Optional: true,
Description: "map of label name to label value for this host",
// suppress differences.
DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool {
return true
},
},
hSummaryStatus: {
Type: schema.TypeString,
Expand Down Expand Up @@ -480,8 +484,9 @@ func resourceMetalHostCreate(d *schema.ResourceData, meta interface{}) (err erro
}

// add tags
if m, ok := (d.Get(hLabels).(map[string]interface{})); ok {
host.Labels = convertMap(m)
if _, ok := (d.Get(hLabels).(map[string]interface{})); ok {
// ignoring labels.
host.Labels = map[string]string{}
}

// Create it
Expand Down Expand Up @@ -783,8 +788,9 @@ func resourceMetalHostUpdate(d *schema.ResourceData, meta interface{}) (err erro
}

// add tags
if m, ok := (d.Get(hLabels).(map[string]interface{})); ok {
updateHost.Labels = convertMap(m)
if _, ok := (d.Get(hLabels).(map[string]interface{})); ok {
// ignoring labels.
updateHost.Labels = map[string]string{}
}

// Update.
Expand Down
27 changes: 25 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,36 @@ func (i InitialiseClient) NewClient(r *schema.ResourceData) (interface{}, error)
// error or is intentional - i.e. if the user has forgotten to add the block or if the
// user doesn't intend to use terraform to run against this service. GetClientFromMetaMap
// below will be used to handle the situation where Client is nil.
metalMap, err := client.GetServiceSettingsMap(constants.ServiceName, r)
metalMap, err := func() (map[string]interface{}, error) {
m, err := client.GetServiceSettingsMap(constants.ServiceName, r)
if err != nil {
if strings.Contains(err.Error(), "service metal block not defined in hpegl stanza") {
m := map[string]interface{}{
"gl_token": false,
}

return m, nil
}

return nil, fmt.Errorf("retrieve service setting: %v", err)
}

return m, nil
}()
if err != nil {
return nil, nil
}

isGLToken := false

if token, ok := metalMap["gl_token"]; ok {
if v, ok := token.(bool); ok && v {
isGLToken = true
}
}

// Initialize the metal client
metalConfig, err := configuration.NewConfig("", configuration.WithGLToken(metalMap["gl_token"].(bool)))
metalConfig, err := configuration.NewConfig("", configuration.WithGLToken(isGLToken))
if err != nil {
return nil, fmt.Errorf("error in creating metal client: %s", err)
}
Expand Down
Loading