|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.commands; |
| 18 | + |
| 19 | +import org.apache.log4j.Logger; |
| 20 | + |
| 21 | +import com.cloud.api.ApiConstants; |
| 22 | +import com.cloud.api.BaseCmd; |
| 23 | +import com.cloud.api.IdentityMapper; |
| 24 | +import com.cloud.api.Implementation; |
| 25 | +import com.cloud.api.Parameter; |
| 26 | +import com.cloud.api.response.FindUserResponse; |
| 27 | +import com.cloud.exception.InvalidParameterValueException; |
| 28 | +import com.cloud.user.User; |
| 29 | + |
| 30 | +@Implementation(description="Find user by name and domain", responseObject=FindUserResponse.class) |
| 31 | +public class FindUserCmd extends BaseCmd { |
| 32 | + public static final Logger s_logger = Logger.getLogger(FindUserCmd.class.getName()); |
| 33 | + |
| 34 | + private static final String s_name = "finduserresponse"; |
| 35 | + |
| 36 | + ///////////////////////////////////////////////////// |
| 37 | + //////////////// API parameters ///////////////////// |
| 38 | + ///////////////////////////////////////////////////// |
| 39 | + |
| 40 | + @Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=true, description="find user with specified username") |
| 41 | + private String username; |
| 42 | + |
| 43 | + @IdentityMapper(entityTableName="domain") |
| 44 | + @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.LONG, required=true, description = "Domain the user belongs to") |
| 45 | + private Long domainId; |
| 46 | + |
| 47 | + ///////////////////////////////////////////////////// |
| 48 | + /////////////////// Accessors /////////////////////// |
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + |
| 51 | + public String getUserName() { |
| 52 | + return username; |
| 53 | + } |
| 54 | + |
| 55 | + public Long getDomainId() { |
| 56 | + return domainId; |
| 57 | + } |
| 58 | + |
| 59 | + ///////////////////////////////////////////////////// |
| 60 | + /////////////// API Implementation/////////////////// |
| 61 | + ///////////////////////////////////////////////////// |
| 62 | + |
| 63 | + @Override |
| 64 | + public String getCommandName() { |
| 65 | + return s_name; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public long getEntityOwnerId() { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void execute(){ |
| 75 | + User result = _accountService.findUser(getUserName(), getDomainId()); |
| 76 | + if(result != null){ |
| 77 | + FindUserResponse response = _responseGenerator.createFindUserResponse(result); |
| 78 | + response.setResponseName(getCommandName()); |
| 79 | + this.setResponseObject(response); |
| 80 | + } else { |
| 81 | + throw new InvalidParameterValueException("User with specified name and domainId does not exist"); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments