8
8
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
9
9
)
10
10
11
+ const tempUserName = "TemporaryUser"
12
+
11
13
// GetDomainByPath fetches a domain by its path.
12
14
func GetDomainByPath (csClient * cloudstack.CloudStackClient , path string ) (string , error , bool ) {
13
15
// Split path and get name.
@@ -113,14 +115,26 @@ func GetOrCreateAccount(csClient *cloudstack.CloudStackClient, account *cloud.Ac
113
115
return err
114
116
}
115
117
118
+ // Attempt to fetch account.
119
+ if resp , count , err := csClient .Account .GetAccountByName (
120
+ account .Name , cloudstack .WithDomain (account .Domain .ID )); err != nil && ! strings .Contains (err .Error (), "No match found" ) {
121
+ return err
122
+ } else if count > 1 {
123
+ return fmt .Errorf ("expected exactly 1 account, but got %d" , count )
124
+ } else if count == 1 {
125
+ account .ID = resp .Id
126
+ return nil
127
+ } // Account not found, do account creation.
128
+
129
+ // Get role for account creation.
116
130
roleDetails , count , err := csClient .Role .GetRoleByName ("Domain Admin" )
117
131
if err != nil {
118
132
return err
119
133
} else if count != 1 {
120
134
return fmt .Errorf ("expected exactly one role with name 'Domain Admin', found %d" , count )
121
135
}
122
136
123
- p := csClient .
Account .
NewCreateAccountParams (
"[email protected] " ,
"first" ,
"last" ,
"temp123" ,
"TempUser" )
137
+ p := csClient .
Account .
NewCreateAccountParams (
"[email protected] " ,
"first" ,
"last" ,
"temp123" ,
tempUserName )
124
138
p .SetDomainid (account .Domain .ID )
125
139
p .SetRoleid (roleDetails .Id )
126
140
resp , err := csClient .Account .CreateAccount (p )
@@ -133,11 +147,44 @@ func GetOrCreateAccount(csClient *cloudstack.CloudStackClient, account *cloud.Ac
133
147
return nil
134
148
}
135
149
136
- // GetOrCreateUser creates a domain as specified in the passed account object.
137
- func GetOrCreateUser (csClient * cloudstack.CloudStackClient , user * cloud.User ) error {
150
+ // GetOrCreateUserWithKey creates a domain as specified in the passed account object.
151
+ // Right now only works with a default TemporaryUser name. This function was only built to get a testing user built.
152
+ func GetOrCreateUserWithKey (csClient * cloudstack.CloudStackClient , user * cloud.User ) error {
138
153
if err := GetOrCreateAccount (csClient , & user .Account ); err != nil {
139
154
return err
140
155
}
141
156
157
+ p := csClient .User .NewListUsersParams ()
158
+ p .SetAccount (user .Account .Name )
159
+ p .SetDomainid (user .Account .Domain .ID )
160
+ if resp , err := csClient .User .ListUsers (p ); err != nil {
161
+ return err
162
+ } else if resp .Count > 1 {
163
+ return fmt .Errorf ("expected exactly one User with name %s, found %d" , user .Name , resp .Count )
164
+ } else if resp .Count == 1 {
165
+ user .ID = resp .Users [0 ].Id
166
+ } else { // User not found, create user.
167
+ // TODO: If ever needed, actually implement user creation here.
168
+ // For now we only care about the default account since this is a testing infrastructure method.
169
+ return fmt .Errorf ("User not found for %s" , user .Name )
170
+ }
171
+
172
+ pGKey := csClient .User .NewGetUserKeysParams (user .ID )
173
+ if resp , err := csClient .User .GetUserKeys (pGKey ); err != nil {
174
+ return err
175
+ } else if user .APIKey != "" {
176
+ user .APIKey = resp .Apikey
177
+ user .SecretKey = resp .Secretkey
178
+ return nil
179
+ }
180
+
181
+ pKey := csClient .User .NewRegisterUserKeysParams (user .ID )
182
+ if resp , err := csClient .User .RegisterUserKeys (pKey ); err != nil {
183
+ return err
184
+ } else {
185
+ user .APIKey = resp .Apikey
186
+ user .SecretKey = resp .Secretkey
187
+ }
188
+
142
189
return nil
143
190
}
0 commit comments