/** * Copyright (C) 2008 Abiquo Holdings S.L. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.abiquo.apiclient; import static com.abiquo.apiclient.domain.ApiPath.ENTERPRISES_URL; import static com.abiquo.apiclient.domain.ApiPath.LOGIN_URL; import static com.abiquo.apiclient.domain.ApiPath.ROLES_URL; import static com.abiquo.apiclient.domain.ApiPath.SCOPES_URL; import static com.abiquo.apiclient.domain.Links.create; import static com.google.common.base.Preconditions.checkNotNull; import java.util.List; import java.util.Map; import com.abiquo.apiclient.domain.options.EnterpriseListOptions; import com.abiquo.apiclient.domain.options.UserListOptions; import com.abiquo.model.enumerator.AuthType; import com.abiquo.server.core.enterprise.EnterpriseDto; import com.abiquo.server.core.enterprise.EnterprisePropertiesDto; import com.abiquo.server.core.enterprise.EnterprisesDto; import com.abiquo.server.core.enterprise.RoleDto; import com.abiquo.server.core.enterprise.RolesDto; import com.abiquo.server.core.enterprise.ScopeDto; import com.abiquo.server.core.enterprise.ScopesDto; import com.abiquo.server.core.enterprise.UserDto; import com.abiquo.server.core.enterprise.UsersDto; import com.abiquo.server.core.infrastructure.PublicCloudCredentialsDto; import com.google.common.base.Joiner; public class EnterpriseApi { private final RestClient client; // Package private constructor to be used only by the ApiClient EnterpriseApi(final RestClient client) { this.client = checkNotNull(client, "client cannot be null"); } public EnterpriseDto createEnterprise(final String name) { EnterpriseDto enterprise = new EnterpriseDto(); enterprise.setName(name); return client.post(ENTERPRISES_URL, EnterpriseDto.MEDIA_TYPE, EnterpriseDto.MEDIA_TYPE, enterprise, EnterpriseDto.class); } public EnterpriseDto editEnterprise(final EnterpriseDto enterprise) { return client.put(enterprise.getEditLink().getHref(), EnterpriseDto.MEDIA_TYPE, EnterpriseDto.MEDIA_TYPE, enterprise, EnterpriseDto.class); } public PublicCloudCredentialsDto addPublicCloudCredentials(final EnterpriseDto enterprise, final PublicCloudCredentialsDto credentials) { return client.post( String.format("%s/%s/credentials/", ENTERPRISES_URL, enterprise.getId()), PublicCloudCredentialsDto.MEDIA_TYPE, PublicCloudCredentialsDto.MEDIA_TYPE, credentials, PublicCloudCredentialsDto.class); } public EnterpriseDto getEnterprise(final String id) { return client .get(ENTERPRISES_URL + "/" + id, EnterpriseDto.MEDIA_TYPE, EnterpriseDto.class); } public Iterable<EnterpriseDto> listEnterprises() { return client.list(ENTERPRISES_URL, EnterprisesDto.MEDIA_TYPE, EnterprisesDto.class); } public Iterable<EnterpriseDto> listEnterprises(final EnterpriseListOptions options) { return client.list(ENTERPRISES_URL, options.queryParams(), EnterprisesDto.MEDIA_TYPE, EnterprisesDto.class); } public ScopeDto createScope(final String name) { ScopeDto scope = new ScopeDto(); scope.setName(name); scope.setAutomaticAddDatacenter(true); scope.setAutomaticAddEnterprise(true); scope.addLink(create("edit", name, SCOPES_URL)); return client.post(SCOPES_URL, ScopeDto.MEDIA_TYPE, ScopeDto.MEDIA_TYPE, scope, ScopeDto.class); } public void deleteUser(final UserDto user) { client.delete(user); } public void deleteScope(final ScopeDto scope) { client.delete(scope); } public UserDto editUser(final UserDto user) { return client.edit(user); } public UserDto createUser(final String name, final String surname, final String nick, final String password, final String email, final String description, final boolean active, final String locale, final AuthType authType, final String publicSshKey, final List<Integer> availableVirtualDatacentersIds, final EnterpriseDto enterprise, final RoleDto role) { UserDto user = new UserDto(); user.setName(name); user.setSurname(surname); user.setNick(nick); user.setPassword(password); user.setEmail(email); user.setDescription(description); user.setActive(active); user.setLocale(locale); user.setAuthType(authType.toString()); user.setPublicSshKey(publicSshKey); user.setAvailableVirtualDatacenters(Joiner.on(",").skipNulls() .join(availableVirtualDatacentersIds)); user.addLink(create("role", role.getEditLink().getHref(), role.getEditLink().getType())); return client.post(enterprise.searchLink("users").getHref(), UserDto.MEDIA_TYPE, UserDto.MEDIA_TYPE, user, UserDto.class); } public UserDto getCurrentUser() { return client.get(LOGIN_URL, UserDto.MEDIA_TYPE, UserDto.class); } public Iterable<UserDto> listUsers(final EnterpriseDto enterprise) { return client.list(enterprise.searchLink("users").getHref(), enterprise.searchLink("users") .getType(), UsersDto.class); } public Iterable<UserDto> listUsers(final EnterpriseDto enterprise, final UserListOptions options) { return client.list(enterprise.searchLink("users").getHref(), options.queryParams(), enterprise.searchLink("users").getType(), UsersDto.class); } public Iterable<RoleDto> listRoles() { return client.list(ROLES_URL, RolesDto.MEDIA_TYPE, RolesDto.class); } public Iterable<ScopeDto> listScopes() { return client.list(SCOPES_URL, ScopesDto.MEDIA_TYPE, ScopesDto.class); } public EnterprisePropertiesDto getEnterpriseProperties(final EnterpriseDto enterprise) { return client.get(enterprise.searchLink("properties").getHref(), EnterprisePropertiesDto.MEDIA_TYPE, EnterprisePropertiesDto.class); } public String getEnterpriseProperty(final EnterpriseDto enterprise, final String key) { EnterprisePropertiesDto entProp = client.get(enterprise.searchLink("properties").getHref(), EnterprisePropertiesDto.MEDIA_TYPE_JSON, EnterprisePropertiesDto.class); return entProp.getProperties().get(key); } public EnterprisePropertiesDto addEnterpriseProperty(final EnterpriseDto enterprise, final Map<String, String> properties) { EnterprisePropertiesDto prop = getEnterpriseProperties(enterprise); prop.getProperties().putAll(properties); return client.put(prop.searchLink("edit").getHref(), EnterprisePropertiesDto.MEDIA_TYPE, EnterprisePropertiesDto.MEDIA_TYPE, prop, EnterprisePropertiesDto.class); } public EnterprisePropertiesDto deleteEnterpriseProperty(final EnterpriseDto enterprise, final List<String> keys) { EnterprisePropertiesDto prop = getEnterpriseProperties(enterprise); for (String key : keys) { prop.getProperties().remove(key); } return client.put(prop.searchLink("edit").getHref(), EnterprisePropertiesDto.MEDIA_TYPE_JSON, EnterprisePropertiesDto.MEDIA_TYPE_JSON, prop, EnterprisePropertiesDto.class); } }