|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package license |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 13 | + "github.com/vmware/govmomi/license" |
| 14 | + "github.com/vmware/govmomi/vim25/methods" |
| 15 | + "github.com/vmware/govmomi/vim25/types" |
| 16 | +) |
| 17 | + |
| 18 | +// MaskLicenseKey returns a masked version of the license key for secure logging. |
| 19 | +// Format: First 4 chars + ****** + last 4 chars |
| 20 | +func MaskLicenseKey(key string) string { |
| 21 | + if len(key) < 9 { |
| 22 | + return "********" |
| 23 | + } |
| 24 | + return key[:4] + strings.Repeat("*", len(key)-8) + key[len(key)-4:] |
| 25 | +} |
| 26 | + |
| 27 | +// MaskedLicenseKeyLogOperation logs license operations with masked key information |
| 28 | +func MaskedLicenseKeyLogOperation(ctx context.Context, operation string, key string, additional map[string]interface{}) { |
| 29 | + if additional == nil { |
| 30 | + additional = make(map[string]interface{}) |
| 31 | + } |
| 32 | + |
| 33 | + additional["masked_key"] = MaskLicenseKey(key) |
| 34 | + additional["key_length"] = len(key) |
| 35 | + |
| 36 | + tflog.Debug(ctx, "License operation: "+operation, additional) |
| 37 | +} |
| 38 | + |
| 39 | +// GetLicenseInfoFromKey retrieves license information based on the provided license key using the license manager. |
| 40 | +func GetLicenseInfoFromKey(ctx context.Context, key string, manager *license.Manager) *types.LicenseManagerLicenseInfo { |
| 41 | + tflog.Debug(ctx, "Attempting to get license info") |
| 42 | + |
| 43 | + tflog.Debug(ctx, "Listing all licenses via license manager") |
| 44 | + infoList, err := manager.List(ctx) |
| 45 | + if err != nil { |
| 46 | + tflog.Error(ctx, "Failed to list licenses from vSphere", map[string]interface{}{ |
| 47 | + "error": err.Error(), |
| 48 | + }) |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + tflog.Debug(ctx, "Iterating through license list to find match", map[string]interface{}{ |
| 53 | + "listSize": len(infoList), |
| 54 | + }) |
| 55 | + for i := range infoList { |
| 56 | + info := infoList[i] |
| 57 | + if info.LicenseKey == key { |
| 58 | + tflog.Debug(ctx, "Found matching license key in list") |
| 59 | + return &info |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + tflog.Debug(ctx, "License key not found in the list") |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// KeyExists checks if a given license key exists within the license manager. |
| 68 | +func KeyExists(ctx context.Context, key string, manager *license.Manager) bool { |
| 69 | + tflog.Debug(ctx, "Checking if license key exists") |
| 70 | + tflog.Debug(ctx, "Listing all licenses via license manager to check existence") |
| 71 | + infoList, err := manager.List(ctx) |
| 72 | + if err != nil { |
| 73 | + tflog.Error(ctx, "Failed to list licenses while checking key existence", map[string]interface{}{ |
| 74 | + "error": err.Error(), |
| 75 | + }) |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + tflog.Debug(ctx, "Iterating through license list to find key", map[string]interface{}{ |
| 80 | + "listSize": len(infoList), |
| 81 | + }) |
| 82 | + for _, info := range infoList { |
| 83 | + if info.LicenseKey == key { |
| 84 | + tflog.Debug(ctx, "Found matching license key") |
| 85 | + return true |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + tflog.Debug(ctx, "License key not found in the list") |
| 90 | + return false |
| 91 | +} |
| 92 | + |
| 93 | +// UpdateLabels updates labels for a specified license key using the provided label map. |
| 94 | +func UpdateLabels(ctx context.Context, manager *license.Manager, licenseKey string, labelMap map[string]interface{}) error { |
| 95 | + tflog.Debug(ctx, "Updating labels for a specific license resource", map[string]interface{}{ |
| 96 | + "labelCount": len(labelMap), |
| 97 | + }) |
| 98 | + |
| 99 | + for key, value := range labelMap { |
| 100 | + stringValue, ok := value.(string) |
| 101 | + if !ok { |
| 102 | + err := fmt.Errorf("label value for key '%s' is not a string (type: %T)", key, value) |
| 103 | + tflog.Error(ctx, "Invalid label value type during update", map[string]interface{}{ |
| 104 | + "labelKey": key, |
| 105 | + "valueType": fmt.Sprintf("%T", value), |
| 106 | + "error": err.Error(), |
| 107 | + }) |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + tflog.Debug(ctx, "Updating individual license label", map[string]interface{}{ |
| 112 | + "labelKey": key, |
| 113 | + "labelValue": stringValue, |
| 114 | + }) |
| 115 | + |
| 116 | + err := UpdateLabel(ctx, manager, licenseKey, key, stringValue) |
| 117 | + if err != nil { |
| 118 | + tflog.Error(ctx, "Failed to update individual license label", map[string]interface{}{ |
| 119 | + "labelKey": key, |
| 120 | + "labelValue": stringValue, |
| 121 | + "error": err.Error(), |
| 122 | + }) |
| 123 | + return fmt.Errorf("failed to update label '%s' for the license resource: %w", key, err) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + tflog.Debug(ctx, "Successfully updated all labels for the license resource") |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +// UpdateLabel assigns or updates the specified label key-value pair for a given license using the license manager. |
| 132 | +func UpdateLabel(ctx context.Context, m *license.Manager, licenseKey string, key string, val string) error { |
| 133 | + tflog.Debug(ctx, "Attempting to update a single license label", map[string]interface{}{ |
| 134 | + "labelKey": key, |
| 135 | + "labelValue": val, |
| 136 | + }) |
| 137 | + |
| 138 | + req := types.UpdateLicenseLabel{ |
| 139 | + This: m.Reference(), |
| 140 | + LicenseKey: licenseKey, |
| 141 | + LabelKey: key, |
| 142 | + LabelValue: val, |
| 143 | + } |
| 144 | + |
| 145 | + _, err := methods.UpdateLicenseLabel(ctx, m.Client(), &req) |
| 146 | + if err != nil { |
| 147 | + tflog.Error(ctx, "Failed API call to update license label", map[string]interface{}{ |
| 148 | + "labelKey": key, |
| 149 | + "labelValue": val, |
| 150 | + "error": err.Error(), |
| 151 | + }) |
| 152 | + return fmt.Errorf("failed to update label '%s': %w", key, err) |
| 153 | + } |
| 154 | + |
| 155 | + tflog.Debug(ctx, "Successfully updated single license label via API", map[string]interface{}{ |
| 156 | + "labelKey": key, |
| 157 | + "labelValue": val, |
| 158 | + }) |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +// DiagnosticError creates an error using the diagnostic property value. |
| 163 | +func DiagnosticError(ctx context.Context, info types.LicenseManagerLicenseInfo) error { |
| 164 | + tflog.Debug(ctx, "Searching for 'diagnostic' property in license info") |
| 165 | + for _, property := range info.Properties { |
| 166 | + tflog.Trace(ctx, "Checking license property", map[string]interface{}{"propertyKey": property.Key}) |
| 167 | + if property.Key == "diagnostic" { |
| 168 | + diagnosticValue, ok := property.Value.(string) |
| 169 | + if !ok { |
| 170 | + err := fmt.Errorf("diagnostic property value is not a string (type: %T)", property.Value) |
| 171 | + tflog.Error(ctx, "Invalid type for diagnostic property value", map[string]interface{}{ |
| 172 | + "valueType": fmt.Sprintf("%T", property.Value), |
| 173 | + "error": err.Error(), |
| 174 | + }) |
| 175 | + return errors.New("failed to process diagnostic property due to unexpected type") |
| 176 | + } |
| 177 | + tflog.Debug(ctx, "Found 'diagnostic' property, creating error from its value", map[string]interface{}{"diagnosticValue": diagnosticValue}) |
| 178 | + return errors.New(diagnosticValue) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + tflog.Debug(ctx, "'diagnostic' property not found in license info") |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +// KeyValuesToMap converts a slice of KeyValue objects into a map with string keys and interface{} values. |
| 187 | +func KeyValuesToMap(ctx context.Context, keyValues []types.KeyValue) map[string]interface{} { |
| 188 | + mapLen := len(keyValues) |
| 189 | + tflog.Debug(ctx, "Converting KeyValue slice to map", map[string]interface{}{"sliceLength": mapLen}) |
| 190 | + |
| 191 | + resultMap := make(map[string]interface{}, mapLen) |
| 192 | + for _, kv := range keyValues { |
| 193 | + tflog.Trace(ctx, "Adding key-value pair to map", map[string]interface{}{"key": kv.Key}) |
| 194 | + resultMap[kv.Key] = kv.Value |
| 195 | + } |
| 196 | + |
| 197 | + tflog.Debug(ctx, "Successfully converted KeyValue slice to map", map[string]interface{}{"mapLength": len(resultMap)}) |
| 198 | + return resultMap |
| 199 | +} |
0 commit comments