Skip to content

Commit

Permalink
vcsim: Implement add/remove license
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongkun Anfernee Gui committed Sep 27, 2017
1 parent afc0002 commit a1f8a32
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion license/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
45 changes: 45 additions & 0 deletions simulator/license_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
81 changes: 81 additions & 0 deletions simulator/license_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit a1f8a32

Please sign in to comment.