Skip to content

Commit f593372

Browse files
author
Joshua Reed
committed
Account creation works now.
1 parent d81c9ad commit f593372

File tree

2 files changed

+88
-50
lines changed

2 files changed

+88
-50
lines changed

test/helpers/user.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func CreateDomainUnderParent(csClient *cloudstack.CloudStackClient, parentID str
5757
}
5858

5959
// GetOrCreateDomain gets or creates a domain as specified in the passed domain object.
60-
func GetOrCreateDomain(domain *cloud.Domain, csClient *cloudstack.CloudStackClient) error {
60+
func GetOrCreateDomain(csClient *cloudstack.CloudStackClient, domain *cloud.Domain) error {
6161
// Split the specified domain path and prepend ROOT/ if it's missing.
6262
domain.Path = strings.Trim(domain.Path, "/")
6363
tokens := strings.Split(domain.Path, "/")
@@ -108,12 +108,37 @@ func DeleteDomain(csClient *cloudstack.CloudStackClient, domainID string) error
108108
return err
109109
}
110110

111-
// // CreateAccount creates a domain as specified in the passed account object.
112-
// func CreateAccount(account *cloud.Account, csClient *cloudstack.CloudStackClient) error {
113-
// return nil
114-
// }
111+
// GetOrCreateAccount creates a domain as specified in the passed account object.
112+
func GetOrCreateAccount(csClient *cloudstack.CloudStackClient, account *cloud.Account) error {
113+
if err := GetOrCreateDomain(csClient, &account.Domain); err != nil {
114+
return err
115+
}
116+
117+
roleDetails, count, err := csClient.Role.GetRoleByName("Domain Admin")
118+
if err != nil {
119+
return err
120+
} else if count != 1 {
121+
return fmt.Errorf("expected exactly one role with name 'Domain Admin', found %d", count)
122+
}
123+
124+
p := csClient.Account.NewCreateAccountParams("[email protected]", "first", "last", "temp123", "TempUser")
125+
p.SetDomainid(account.Domain.ID)
126+
p.SetRoleid(roleDetails.Id)
127+
resp, err := csClient.Account.CreateAccount(p)
128+
if err != nil {
129+
return err
130+
}
131+
account.Name = resp.Name
132+
account.ID = resp.Id
115133

116-
// // CreateUser creates a domain as specified in the passed account object.
117-
// func CreateUser(user *cloud.User, csClient *cloudstack.CloudStackClient) error {
118-
// return nil
119-
// }
134+
return nil
135+
}
136+
137+
// GetOrCreateUser creates a domain as specified in the passed account object.
138+
func GetOrCreateUser(csClient *cloudstack.CloudStackClient, user *cloud.User) error {
139+
if err := GetOrCreateAccount(csClient, &user.Account); err != nil {
140+
return err
141+
}
142+
143+
return nil
144+
}

test/helpers/user_test.go

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,36 @@ import (
2727
)
2828

2929
var _ = Describe("Test helper methods", func() {
30-
31-
Context("Domain Creation and Deletion Integ Tests", func() {
32-
var csClient *cloudstack.CloudStackClient
33-
ccPath := "../../cloud-config"
34-
var connectionErr error
35-
conf := cloud.Config{}
36-
if rawCfg, err := ini.Load("../../cloud-config"); err != nil {
37-
connectionErr = errors.Wrapf(err, "reading config at path %s:", ccPath)
38-
} else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 {
39-
connectionErr = errors.New("section Global not found")
40-
} else if err = rawCfg.Section("Global").StrictMapTo(&conf); err != nil {
41-
connectionErr = errors.Wrapf(err, "parsing [Global] section from config at path %s:", ccPath)
42-
}
43-
44-
csClient = cloudstack.NewAsyncClient(conf.APIURL, conf.APIKey, conf.SecretKey, conf.VerifySSL)
45-
46-
// Get the root domain's ID.
47-
rootDomainID, err, found := helpers.GetDomainByPath(csClient, "ROOT/")
48-
Ω(err).ShouldNot(HaveOccurred())
49-
Ω(rootDomainID).ShouldNot(BeEmpty())
50-
Ω(found).Should(BeTrue())
51-
52-
BeforeEach(func() {
53-
if connectionErr != nil { // Only do these tests if an actual ACS instance is available via cloud-config.
54-
Skip("Could not connect to ACS instance.")
55-
}
56-
})
57-
58-
AfterEach(func() {
59-
for _, path := range []string{"ROOT/someNewDomain", "ROOT/blah"} {
60-
// Delete any created domains.
61-
id, err, found := helpers.GetDomainByPath(csClient, path)
62-
Ω(err).ShouldNot(HaveOccurred())
63-
if found {
64-
Ω(helpers.DeleteDomain(csClient, id)).Should(Succeed())
65-
}
30+
var csClient *cloudstack.CloudStackClient
31+
ccPath := "../../cloud-config"
32+
conf := cloud.Config{}
33+
if rawCfg, err := ini.Load("../../cloud-config"); err != nil {
34+
Ω(errors.Wrapf(err, "reading config at path %s:", ccPath)).ShouldNot(HaveOccurred())
35+
} else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 {
36+
Ω(errors.New("section Global not found")).ShouldNot(HaveOccurred())
37+
} else if err = rawCfg.Section("Global").StrictMapTo(&conf); err != nil {
38+
Ω(errors.Wrapf(err, "parsing [Global] section from config at path %s:", ccPath)).ShouldNot(HaveOccurred())
39+
}
40+
csClient = cloudstack.NewAsyncClient(conf.APIURL, conf.APIKey, conf.SecretKey, conf.VerifySSL)
41+
42+
// Get the root domain's ID.
43+
rootDomainID, err, found := helpers.GetDomainByPath(csClient, "ROOT/")
44+
Ω(err).ShouldNot(HaveOccurred())
45+
Ω(rootDomainID).ShouldNot(BeEmpty())
46+
Ω(found).Should(BeTrue())
47+
48+
AfterEach(func() {
49+
for _, path := range []string{"ROOT/someNewDomain", "ROOT/blah"} {
50+
// Delete any created domains.
51+
id, err, found := helpers.GetDomainByPath(csClient, path)
52+
Ω(err).ShouldNot(HaveOccurred())
53+
if found {
54+
Ω(helpers.DeleteDomain(csClient, id)).Should(Succeed())
6655
}
67-
})
56+
}
57+
})
6858

59+
Context("Domain Creation and Deletion.", func() {
6960
It("Can get the ROOT domain's ID.", func() {
7061
id, err, found := helpers.GetDomainByPath(csClient, "ROOT/")
7162
Ω(err).ShouldNot(HaveOccurred())
@@ -88,7 +79,7 @@ var _ = Describe("Test helper methods", func() {
8879

8980
It("Returns an appropriate error when the domain already exists.", func() {
9081
someDomain := &cloud.Domain{Name: "blah", Path: "blah"}
91-
Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed())
82+
Ω(helpers.GetOrCreateDomain(csClient, someDomain)).Should(Succeed())
9283
Ω(someDomain.Name).Should(Equal("blah"))
9384
Ω(someDomain.Path).Should(Equal("ROOT/blah"))
9485
Ω(someDomain.ID).ShouldNot(BeEmpty())
@@ -99,23 +90,45 @@ var _ = Describe("Test helper methods", func() {
9990

10091
It("Doesn't error if the domain already exists.", func() {
10192
someDomain := &cloud.Domain{Name: "blah", Path: "blah"}
102-
Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed())
93+
Ω(helpers.GetOrCreateDomain(csClient, someDomain)).Should(Succeed())
10394
Ω(someDomain.Name).Should(Equal("blah"))
10495
Ω(someDomain.Path).Should(Equal("ROOT/blah"))
10596
Ω(someDomain.ID).ShouldNot(BeEmpty())
10697

107-
Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed())
98+
Ω(helpers.GetOrCreateDomain(csClient, someDomain)).Should(Succeed())
10899
Ω(someDomain.Name).Should(Equal("blah"))
109100
Ω(someDomain.Path).Should(Equal("ROOT/blah"))
110101
Ω(someDomain.ID).ShouldNot(BeEmpty())
111102
})
112103

113104
It("Can create a wholly new multi-level sub-domain path.", func() {
114105
someDomain := &cloud.Domain{Name: "tooBlah", Path: "ROOT/someNewDomain/tooBlah"}
115-
Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed())
106+
Ω(helpers.GetOrCreateDomain(csClient, someDomain)).Should(Succeed())
116107
Ω(someDomain.Name).Should(Equal("tooBlah"))
117108
Ω(someDomain.Path).Should(Equal("ROOT/someNewDomain/tooBlah"))
118109
Ω(someDomain.ID).ShouldNot(BeEmpty())
119110
})
120111
})
112+
113+
Context("Account Creation.", func() {
114+
It("Can create a new account in a new domain.", func() {
115+
domain := cloud.Domain{Path: "ROOT/someNewDomain/tooBlah"}
116+
account := cloud.Account{Name: "TempTestAccount", Domain: domain}
117+
Ω(helpers.GetOrCreateAccount(csClient, &account)).Should(Succeed())
118+
})
119+
It("Doesn't fail if the account already exists.", func() {
120+
domain := cloud.Domain{Path: "ROOT/someNewDomain/tooBlah"}
121+
account := cloud.Account{Name: "TempTestAccount", Domain: domain}
122+
Ω(helpers.GetOrCreateAccount(csClient, &account)).Should(Succeed())
123+
Ω(helpers.GetOrCreateAccount(csClient, &account)).Should(Succeed())
124+
})
125+
})
126+
127+
// Context("User Creation w/Keys.", func() {
128+
// It("Can create a new account in a new domain.", func() {
129+
// domain := cloud.Domain{Path: "ROOT/someNewDomain/tooBlah"}
130+
// account := cloud.Account{Name: "TempTestAccount", Domain: domain}
131+
// Ω(helpers.GetOrCreateAccount(csClient, &account)).Should(Succeed())
132+
// })
133+
// })
121134
})

0 commit comments

Comments
 (0)