AMBARI-21029. Integrate SettingService with Swagger (adoroszlai)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/51fc3cf7 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/51fc3cf7 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/51fc3cf7 Branch: refs/heads/trunk Commit: 51fc3cf77302c7bc04ffb9fdbd53f053b053514f Parents: 0177263 Author: Attila Doroszlai <[email protected]> Authored: Tue May 23 11:35:31 2017 +0200 Committer: Attila Doroszlai <[email protected]> Committed: Tue May 23 11:35:31 2017 +0200 ---------------------------------------------------------------------- .../api/services/SettingRequestSwagger.java | 30 +++++ .../server/api/services/SettingService.java | 126 ++++++++++++++++--- .../server/controller/SettingRequest.java | 69 ++++++++++ .../server/controller/SettingResponse.java | 90 +++++++++++++ .../internal/SettingResourceProvider.java | 64 ++++++---- .../server/api/services/SettingServiceTest.java | 2 +- 6 files changed, 337 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/51fc3cf7/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingRequestSwagger.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingRequestSwagger.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingRequestSwagger.java new file mode 100644 index 0000000..4e854a4 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingRequestSwagger.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.api.services; + +import org.apache.ambari.server.controller.ApiModel; +import org.apache.ambari.server.controller.SettingRequest; +import org.apache.ambari.server.controller.internal.SettingResourceProvider; + +import io.swagger.annotations.ApiModelProperty; + +@SuppressWarnings("unused") // for Swagger +public interface SettingRequestSwagger extends ApiModel { + @ApiModelProperty(name = SettingResourceProvider.RESPONSE_KEY) + SettingRequest getSettingRequest(); +} http://git-wip-us.apache.org/repos/asf/ambari/blob/51fc3cf7/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingService.java index e186be2..b8da678 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/SettingService.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -29,18 +29,35 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.apache.ambari.server.api.resources.ResourceInstance; +import org.apache.ambari.server.controller.SettingResponse.SettingResponseWrapper; +import org.apache.ambari.server.controller.internal.SettingResourceProvider; import org.apache.ambari.server.controller.spi.Resource; +import org.apache.http.HttpStatus; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; /** * Service responsible for setting resource requests. */ -@Path("/settings/") +@Path("/settings") +@Api(value = "/settings", description = "Endpoint for settings-specific operations") public class SettingService extends BaseService { + private static final String DEFAULT_FIELDS_GET_SETTINGS = SettingResourceProvider.SETTING_NAME_PROPERTY_ID; + private static final String DEFAULT_FIELDS_GET_SETTING = SettingResourceProvider.ALL_PROPERTIES; + private static final String SETTING_REQUEST_TYPE = "org.apache.ambari.server.api.services.SettingRequestSwagger"; + /** * Construct a SettingService. */ @@ -58,9 +75,26 @@ public class SettingService extends BaseService { * @return setting collection resource representation */ @GET - @Produces("text/plain") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Returns all settings", + response = SettingResponseWrapper.class, responseContainer = RESPONSE_CONTAINER_LIST) + @ApiImplicitParams({ + @ApiImplicitParam(name = QUERY_FIELDS, value = QUERY_FILTER_DESCRIPTION, dataType = DATA_TYPE_STRING, paramType = PARAM_TYPE_QUERY, + defaultValue = DEFAULT_FIELDS_GET_SETTINGS), + @ApiImplicitParam(name = QUERY_SORT, value = QUERY_SORT_DESCRIPTION, dataType = DATA_TYPE_STRING, paramType = PARAM_TYPE_QUERY), + @ApiImplicitParam(name = QUERY_PAGE_SIZE, value = QUERY_PAGE_SIZE_DESCRIPTION, defaultValue = DEFAULT_PAGE_SIZE, dataType = DATA_TYPE_INT, paramType = PARAM_TYPE_QUERY), + @ApiImplicitParam(name = QUERY_FROM, value = QUERY_FROM_DESCRIPTION, allowableValues = QUERY_FROM_VALUES, defaultValue = DEFAULT_FROM, dataType = DATA_TYPE_INT, paramType = PARAM_TYPE_QUERY), + @ApiImplicitParam(name = QUERY_TO, value = QUERY_TO_DESCRIPTION, allowableValues = QUERY_TO_VALUES, dataType = QUERY_TO_TYPE, paramType = PARAM_TYPE_QUERY), + }) + @ApiResponses({ + @ApiResponse(code = HttpStatus.SC_OK, message = MSG_SUCCESSFUL_OPERATION), + @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = MSG_NOT_AUTHENTICATED), + @ApiResponse(code = HttpStatus.SC_FORBIDDEN, message = MSG_PERMISSION_DENIED), + @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = MSG_SERVER_ERROR), + }) public Response getSettings(String body, @Context HttpHeaders headers, @Context UriInfo ui) { - return handleRequest(headers, body, ui, Request.Type.GET, createSettingResource(null)); + ResourceInstance resource = createSettingResource(null); + return handleRequest(headers, body, ui, Request.Type.GET, resource); } /** @@ -75,14 +109,33 @@ public class SettingService extends BaseService { */ @GET @Path("{settingName}") - @Produces("text/plain") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Returns a specific setting", + response = SettingResponseWrapper.class) + @ApiImplicitParams({ + @ApiImplicitParam(name = QUERY_FIELDS, value = QUERY_FILTER_DESCRIPTION, dataType = DATA_TYPE_STRING, paramType = PARAM_TYPE_QUERY, + defaultValue = DEFAULT_FIELDS_GET_SETTING), + @ApiImplicitParam(name = QUERY_SORT, value = QUERY_SORT_DESCRIPTION, dataType = DATA_TYPE_STRING, paramType = PARAM_TYPE_QUERY), + @ApiImplicitParam(name = QUERY_PAGE_SIZE, value = QUERY_PAGE_SIZE_DESCRIPTION, defaultValue = DEFAULT_PAGE_SIZE, dataType = DATA_TYPE_INT, paramType = PARAM_TYPE_QUERY), + @ApiImplicitParam(name = QUERY_FROM, value = QUERY_FROM_DESCRIPTION, allowableValues = QUERY_FROM_VALUES, defaultValue = DEFAULT_FROM, dataType = DATA_TYPE_INT, paramType = PARAM_TYPE_QUERY), + @ApiImplicitParam(name = QUERY_TO, value = QUERY_TO_DESCRIPTION, allowableValues = QUERY_TO_VALUES, dataType = QUERY_TO_TYPE, paramType = PARAM_TYPE_QUERY), + }) + @ApiResponses({ + @ApiResponse(code = HttpStatus.SC_OK, message = MSG_SUCCESSFUL_OPERATION), + @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = MSG_RESOURCE_NOT_FOUND), + @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = MSG_NOT_AUTHENTICATED), + @ApiResponse(code = HttpStatus.SC_FORBIDDEN, message = MSG_PERMISSION_DENIED), + @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = MSG_SERVER_ERROR), + }) public Response getSetting(String body, @Context HttpHeaders headers, @Context UriInfo ui, - @PathParam("settingName") String settingName) { - return handleRequest(headers, body, ui, Request.Type.GET, createSettingResource(settingName)); + @ApiParam(value = "setting name", required = true) @PathParam("settingName") String settingName + ) { + ResourceInstance resource = createSettingResource(settingName); + return handleRequest(headers, body, ui, Request.Type.GET, resource); } /** - * Handles: POST /settings/{settingName} + * Handles: POST /settings * Create a specific setting. * * @param headers http headers @@ -91,9 +144,24 @@ public class SettingService extends BaseService { * @return information regarding the created setting */ @POST - @Produces("text/plain") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Creates a setting") + @ApiImplicitParams({ + @ApiImplicitParam(dataType = SETTING_REQUEST_TYPE, paramType = PARAM_TYPE_BODY, required = true) + }) + @ApiResponses({ + @ApiResponse(code = HttpStatus.SC_CREATED, message = MSG_SUCCESSFUL_OPERATION), + @ApiResponse(code = HttpStatus.SC_ACCEPTED, message = MSG_REQUEST_ACCEPTED), + @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = MSG_INVALID_ARGUMENTS), + @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = MSG_CLUSTER_NOT_FOUND), + @ApiResponse(code = HttpStatus.SC_CONFLICT, message = MSG_RESOURCE_ALREADY_EXISTS), + @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = MSG_NOT_AUTHENTICATED), + @ApiResponse(code = HttpStatus.SC_FORBIDDEN, message = MSG_PERMISSION_DENIED), + @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = MSG_SERVER_ERROR), + }) public Response createSetting(String body, @Context HttpHeaders headers, @Context UriInfo ui) { - return handleRequest(headers, body, ui, Request.Type.POST, createSettingResource(null)); + ResourceInstance resource = createSettingResource(null); + return handleRequest(headers, body, ui, Request.Type.POST, resource); } /** @@ -108,10 +176,23 @@ public class SettingService extends BaseService { */ @PUT @Path("{settingName}") - @Produces("text/plain") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Updates a setting") + @ApiImplicitParams({ + @ApiImplicitParam(dataType = SETTING_REQUEST_TYPE, paramType = PARAM_TYPE_BODY, required = true) + }) + @ApiResponses({ + @ApiResponse(code = HttpStatus.SC_OK, message = MSG_SUCCESSFUL_OPERATION), + @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = MSG_RESOURCE_NOT_FOUND), + @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = MSG_NOT_AUTHENTICATED), + @ApiResponse(code = HttpStatus.SC_FORBIDDEN, message = MSG_PERMISSION_DENIED), + @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = MSG_SERVER_ERROR), + }) public Response updateSetting(String body, @Context HttpHeaders headers, @Context UriInfo ui, - @PathParam("settingName") String settingName) { - return handleRequest(headers, body, ui, Request.Type.PUT, createSettingResource(settingName)); + @ApiParam(value = "setting name", required = true) @PathParam("settingName") String settingName + ) { + ResourceInstance resource = createSettingResource(settingName); + return handleRequest(headers, body, ui, Request.Type.PUT, resource); } /** @@ -126,10 +207,20 @@ public class SettingService extends BaseService { */ @DELETE @Path("{settingName}") - @Produces("text/plain") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Deletes a setting") + @ApiResponses({ + @ApiResponse(code = HttpStatus.SC_OK, message = MSG_SUCCESSFUL_OPERATION), + @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = MSG_RESOURCE_NOT_FOUND), + @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = MSG_NOT_AUTHENTICATED), + @ApiResponse(code = HttpStatus.SC_FORBIDDEN, message = MSG_PERMISSION_DENIED), + @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = MSG_SERVER_ERROR), + }) public Response deleteSetting(@Context HttpHeaders headers, @Context UriInfo ui, - @PathParam("settingName") String settingName) { - return handleRequest(headers, null, ui, Request.Type.DELETE, createSettingResource(settingName)); + @ApiParam(value = "setting name", required = true) @PathParam("settingName") String settingName + ) { + ResourceInstance resource = createSettingResource(settingName); + return handleRequest(headers, null, ui, Request.Type.DELETE, resource); } // ----- helper methods ---------------------------------------------------- @@ -141,8 +232,9 @@ public class SettingService extends BaseService { * * @return a setting resource instance */ - ResourceInstance createSettingResource(String settingName) { + protected ResourceInstance createSettingResource(String settingName) { return createResource(Resource.Type.Setting, Collections.singletonMap(Resource.Type.Setting, settingName)); } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/51fc3cf7/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingRequest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingRequest.java new file mode 100644 index 0000000..4331ba3 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingRequest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.controller; + +import java.util.Objects; + +import org.apache.ambari.server.controller.internal.SettingResourceProvider; + +import io.swagger.annotations.ApiModelProperty; + +public class SettingRequest { + + private final String name; + private final String settingType; + private final String content; + + public SettingRequest(String name, String settingType, String content) { + this.name = name; + this.settingType = settingType; + this.content = content; + } + + @ApiModelProperty(name = SettingResourceProvider.NAME) + public String getName() { + return name; + } + + @ApiModelProperty(name = SettingResourceProvider.SETTING_TYPE) + public String getSettingType() { + return settingType; + } + + @ApiModelProperty(name = SettingResourceProvider.CONTENT) + public String getContent() { + return content; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + SettingRequest other = (SettingRequest) o; + return Objects.equals(name, other.name) && + Objects.equals(settingType, other.settingType) && + Objects.equals(content, other.content); + } + + @Override + public int hashCode() { + return Objects.hash(name, settingType, content); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/51fc3cf7/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingResponse.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingResponse.java new file mode 100644 index 0000000..cd51184 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/SettingResponse.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.controller; + +import java.util.Objects; + +import org.apache.ambari.server.controller.internal.SettingResourceProvider; + +import io.swagger.annotations.ApiModelProperty; + +public class SettingResponse { + + private final String name; + private final String settingType; + private final String content; + private final String updatedBy; + private final long updateTimestamp; + + public SettingResponse(String name, String settingType, String content, String updatedBy, long updateTimestamp) { + this.name = name; + this.settingType = settingType; + this.content = content; + this.updatedBy = updatedBy; + this.updateTimestamp = updateTimestamp; + } + + @ApiModelProperty(name = SettingResourceProvider.NAME) + public String getName() { + return name; + } + + @ApiModelProperty(name = SettingResourceProvider.SETTING_TYPE) + public String getSettingType() { + return settingType; + } + + @ApiModelProperty(name = SettingResourceProvider.CONTENT) + public String getContent() { + return content; + } + + @ApiModelProperty(name = SettingResourceProvider.UPDATED_BY) + public String getUpdatedBy() { + return updatedBy; + } + + @ApiModelProperty(name = SettingResourceProvider.UPDATE_TIMESTAMP) + public long getUpdateTimestamp() { + return updateTimestamp; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + SettingResponse other = (SettingResponse) o; + return Objects.equals(name, other.name) && + Objects.equals(settingType, other.settingType) && + Objects.equals(content, other.content) && + Objects.equals(updatedBy, other.updatedBy) && + updateTimestamp == other.updateTimestamp; + } + + @Override + public int hashCode() { + return Objects.hash(name, settingType, content, updatedBy, updateTimestamp); + } + + public interface SettingResponseWrapper extends ApiModel { + @ApiModelProperty(name = SettingResourceProvider.RESPONSE_KEY) + @SuppressWarnings("unused") + SettingResponse getSettingResponse(); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/51fc3cf7/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SettingResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SettingResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SettingResourceProvider.java index e50490e..5235ef4 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SettingResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SettingResourceProvider.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -29,6 +29,7 @@ import java.util.Set; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.DuplicateResourceException; import org.apache.ambari.server.StaticallyInject; +import org.apache.ambari.server.controller.SettingResponse; import org.apache.ambari.server.controller.spi.NoSuchParentResourceException; import org.apache.ambari.server.controller.spi.NoSuchResourceException; import org.apache.ambari.server.controller.spi.Predicate; @@ -46,6 +47,7 @@ import org.apache.ambari.server.security.authorization.RoleAuthorization; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; +import com.google.common.collect.ImmutableSet; import com.google.inject.Inject; /** @@ -54,14 +56,20 @@ import com.google.inject.Inject; @StaticallyInject public class SettingResourceProvider extends AbstractAuthorizedResourceProvider { - private static final String SETTINGS = "Settings"; + public static final String RESPONSE_KEY = "Settings"; protected static final String ID = "id"; protected static final String SETTING = "Setting"; - protected static final String SETTING_NAME_PROPERTY_ID = PropertyHelper.getPropertyId(SETTINGS, "name"); - protected static final String SETTING_SETTING_TYPE_PROPERTY_ID = PropertyHelper.getPropertyId(SETTINGS, "setting_type"); - protected static final String SETTING_CONTENT_PROPERTY_ID = PropertyHelper.getPropertyId(SETTINGS, "content"); - protected static final String SETTING_UPDATED_BY_PROPERTY_ID = PropertyHelper.getPropertyId(SETTINGS, "updated_by"); - protected static final String SETTING_UPDATE_TIMESTAMP_PROPERTY_ID = PropertyHelper.getPropertyId(SETTINGS, "update_timestamp"); + public static final String NAME = "name"; + public static final String SETTING_TYPE = "setting_type"; + public static final String CONTENT = "content"; + public static final String UPDATED_BY = "updated_by"; + public static final String UPDATE_TIMESTAMP = "update_timestamp"; + public static final String SETTING_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + NAME; + public static final String SETTING_SETTING_TYPE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + SETTING_TYPE; + public static final String SETTING_CONTENT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + CONTENT; + public static final String SETTING_UPDATED_BY_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + UPDATED_BY; + public static final String SETTING_UPDATE_TIMESTAMP_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + UPDATE_TIMESTAMP; + public static final String ALL_PROPERTIES = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + "*"; /** * The property ids for setting resource. @@ -73,7 +81,11 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider */ private static final Map<Resource.Type, String> keyPropertyIds = new HashMap<>(); - private static final Set<String> requiredProperties = new HashSet<>(); + private static final Set<String> REQUIRED_PROPERTIES = ImmutableSet.of( + SETTING_NAME_PROPERTY_ID, + SETTING_SETTING_TYPE_PROPERTY_ID, + SETTING_CONTENT_PROPERTY_ID + ); @Inject private static SettingDAO dao; @@ -88,10 +100,6 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider propertyIds.add(SETTING); keyPropertyIds.put(Resource.Type.Setting, SETTING_NAME_PROPERTY_ID); - - requiredProperties.add(SETTING_NAME_PROPERTY_ID); - requiredProperties.add(SETTING_SETTING_TYPE_PROPERTY_ID); - requiredProperties.add(SETTING_CONTENT_PROPERTY_ID); } protected SettingResourceProvider() { @@ -113,9 +121,9 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider Set<Resource> associatedResources = new HashSet<>(); for (Map<String, Object> properties : request.getProperties()) { - SettingEntity settingEntity = createResources(newCreateCommand(request, properties)); + SettingResponse setting = createResources(newCreateCommand(request, properties)); Resource resource = new ResourceImpl(Resource.Type.Setting); - resource.setProperty(SETTING_NAME_PROPERTY_ID, settingEntity.getName()); + resource.setProperty(SETTING_NAME_PROPERTY_ID, setting.getName()); associatedResources.add(resource); } @@ -145,7 +153,7 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<>(); for(SettingEntity entity : entities) { - resources.add(toResource(entity, requestedIds)); + resources.add(toResource(toResponse(entity), requestedIds)); } return resources; } @@ -169,10 +177,10 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider } - private Command<SettingEntity> newCreateCommand(final Request request, final Map<String, Object> properties) { - return new Command<SettingEntity>() { + private Command<SettingResponse> newCreateCommand(final Request request, final Map<String, Object> properties) { + return new Command<SettingResponse>() { @Override - public SettingEntity invoke() throws AmbariException, AuthorizationException { + public SettingResponse invoke() throws AmbariException, AuthorizationException { SettingEntity entity = toEntity(properties); if (dao.findByName(entity.getName()) != null) { throw new DuplicateResourceException( @@ -180,7 +188,7 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider } dao.create(entity); notifyCreate(Resource.Type.Setting, request); - return entity; + return toResponse(entity); } }; } @@ -224,18 +232,18 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider entity.setUpdateTimestamp(System.currentTimeMillis()); } - private Resource toResource(final SettingEntity settingEntity, final Set<String> requestedIds) { + private Resource toResource(final SettingResponse setting, final Set<String> requestedIds) { Resource resource = new ResourceImpl(Resource.Type.Setting); - setResourceProperty(resource, SETTING_NAME_PROPERTY_ID, settingEntity.getName(), requestedIds); - setResourceProperty(resource, SETTING_SETTING_TYPE_PROPERTY_ID, settingEntity.getSettingType(), requestedIds); - setResourceProperty(resource, SETTING_CONTENT_PROPERTY_ID, settingEntity.getContent(), requestedIds); - setResourceProperty(resource, SETTING_UPDATED_BY_PROPERTY_ID, settingEntity.getUpdatedBy(), requestedIds); - setResourceProperty(resource, SETTING_UPDATE_TIMESTAMP_PROPERTY_ID, settingEntity.getUpdateTimestamp(), requestedIds); + setResourceProperty(resource, SETTING_NAME_PROPERTY_ID, setting.getName(), requestedIds); + setResourceProperty(resource, SETTING_SETTING_TYPE_PROPERTY_ID, setting.getSettingType(), requestedIds); + setResourceProperty(resource, SETTING_CONTENT_PROPERTY_ID, setting.getContent(), requestedIds); + setResourceProperty(resource, SETTING_UPDATED_BY_PROPERTY_ID, setting.getUpdatedBy(), requestedIds); + setResourceProperty(resource, SETTING_UPDATE_TIMESTAMP_PROPERTY_ID, setting.getUpdateTimestamp(), requestedIds); return resource; } private SettingEntity toEntity(final Map<String, Object> properties) throws AmbariException { - for (String propertyName: requiredProperties) { + for (String propertyName: REQUIRED_PROPERTIES) { if (properties.get(propertyName) == null) { throw new AmbariException(String.format("Property %s should be provided", propertyName)); } @@ -249,4 +257,8 @@ public class SettingResourceProvider extends AbstractAuthorizedResourceProvider entity.setUpdateTimestamp(System.currentTimeMillis()); return entity; } + + private static SettingResponse toResponse(SettingEntity entity) { + return new SettingResponse(entity.getName(), entity.getSettingType(), entity.getContent(), entity.getUpdatedBy(), entity.getUpdateTimestamp()); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/51fc3cf7/ambari-server/src/test/java/org/apache/ambari/server/api/services/SettingServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/SettingServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/SettingServiceTest.java index 1bc75ff..3af8751 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/SettingServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/SettingServiceTest.java @@ -79,7 +79,7 @@ public class SettingServiceTest extends BaseServiceTest { } @Override - ResourceInstance createSettingResource(String settingName) { + protected ResourceInstance createSettingResource(String settingName) { assertEquals(this.settingName, settingName); return getTestResource(); }
