diff --git a/license/manager.go b/license/manager.go index 9c38e9147..4ba66772d 100644 --- a/license/manager.go +++ b/license/manager.go @@ -41,7 +41,7 @@ func NewManager(c *vim25.Client) *Manager { } func mapToKeyValueSlice(m map[string]string) []types.KeyValue { - r := make([]types.KeyValue, len(m)) + var r []types.KeyValue for k, v := range m { r = append(r, types.KeyValue{Key: k, Value: v}) } diff --git a/simulator/license_manager.go b/simulator/license_manager.go index adabfc54b..13565d32e 100644 --- a/simulator/license_manager.go +++ b/simulator/license_manager.go @@ -77,6 +77,42 @@ func NewLicenseManager(ref types.ManagedObjectReference) object.Reference { return m } +func (m *LicenseManager) AddLicense(req *types.AddLicense) soap.HasFault { + body := &methods.AddLicenseBody{ + Res: &types.AddLicenseResponse{}, + } + + for _, license := range m.Licenses { + if license.LicenseKey == req.LicenseKey { + body.Res.Returnval = licenseInfo(license.LicenseKey, license.Labels) + return body + } + } + + m.Licenses = append(m.Licenses, types.LicenseManagerLicenseInfo{ + LicenseKey: req.LicenseKey, + Labels: req.Labels, + }) + + body.Res.Returnval = licenseInfo(req.LicenseKey, req.Labels) + + return body +} + +func (m *LicenseManager) RemoveLicense(req *types.RemoveLicense) soap.HasFault { + body := &methods.RemoveLicenseBody{ + Res: &types.RemoveLicenseResponse{}, + } + + for i, license := range m.Licenses { + if req.LicenseKey == license.LicenseKey { + m.Licenses = append(m.Licenses[:i], m.Licenses[i+1:]...) + return body + } + } + return body +} + type LicenseAssignmentManager struct { mo.LicenseAssignmentManager } @@ -109,3 +145,12 @@ func (m *LicenseAssignmentManager) QueryAssignedLicenses(req *types.QueryAssigne return body } + +func licenseInfo(key string, labels []types.KeyValue) types.LicenseManagerLicenseInfo { + info := EvalLicense + + info.LicenseKey = key + info.Labels = labels + + return info +} diff --git a/simulator/license_manager_test.go b/simulator/license_manager_test.go index 8303f4dd2..e0491cd35 100644 --- a/simulator/license_manager_test.go +++ b/simulator/license_manager_test.go @@ -123,3 +123,84 @@ func TestLicenseManagerESX(t *testing.T) { t.Fatal("invalid license") } } + +func TestAddRemoveLicense(t *testing.T) { + ctx := context.Background() + m := ESX() + + defer m.Remove() + + err := m.Create() + if err != nil { + t.Fatal(err) + } + + s := m.Service.NewServer() + defer s.Close() + + c, err := govmomi.NewClient(ctx, s.URL, true) + if err != nil { + t.Fatal(err) + } + + lm := license.NewManager(c.Client) + key := "00000-00000-00000-00000-11111" + labels := map[string]string{"key": "value"} + + info, err := lm.Add(ctx, key, labels) + if err != nil { + t.Fatal(err) + } + + if info.LicenseKey != key { + t.Fatalf("expect info.LicenseKey equal to %q; got %q", key, info.LicenseKey) + } + + if len(info.Labels) != len(labels) { + t.Fatalf("expect len(info.Labels) eqaul to %d; got %d", + len(labels), len(info.Labels)) + } + + if info.Labels[0].Key != "key" || info.Labels[0].Value != "value" { + t.Fatalf("expect label to be {key:value}; got {%s:%s}", + info.Labels[0].Key, info.Labels[0].Value) + } + + la, err := lm.List(ctx) + if err != nil { + t.Fatal(err) + } + + if len(la) != 2 { + t.Fatal("no licenses") + } + + if la[1].LicenseKey != key { + t.Fatalf("expect info.LicenseKey equal to %q; got %q", + key, la[1].LicenseKey) + } + + if len(la[1].Labels) != len(labels) { + t.Fatalf("expect len(info.Labels) eqaul to %d; got %d", + len(labels), len(la[1].Labels)) + } + + if la[1].Labels[0].Key != "key" || la[1].Labels[0].Value != "value" { + t.Fatalf("expect label to be {key:value}; got {%s:%s}", + la[1].Labels[0].Key, la[1].Labels[0].Value) + } + + err = lm.Remove(ctx, key) + if err != nil { + t.Fatal(err) + } + + la, err = lm.List(ctx) + if err != nil { + t.Fatal(err) + } + + if len(la) != 1 { + t.Fatal("no licenses") + } +}