Search in sources :

Example 1 with IdCredentials

use of com.cloudbees.plugins.credentials.common.IdCredentials in project blueocean-plugin by jenkinsci.

the class CredentialApi method create.

@POST
@WebMethod(name = "")
public CreateResponse create(@JsonBody JSONObject body, StaplerRequest request) throws IOException {
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("No authenticated user found");
    }
    JSONObject jsonObject = body.getJSONObject("credentials");
    final IdCredentials credentials = request.bindJSON(IdCredentials.class, jsonObject);
    String domainName = DOMAIN_NAME;
    if (jsonObject.get("domain") != null && jsonObject.get("domain") instanceof String) {
        domainName = (String) jsonObject.get("domain");
    }
    CredentialsUtils.createCredentialsInUserStore(credentials, authenticatedUser, domainName, ImmutableList.<DomainSpecification>of(new BlueOceanDomainSpecification()));
    CredentialsStoreAction.DomainWrapper domainWrapper = credentialStoreAction.getDomain(domainName);
    if (domainWrapper != null) {
        CredentialsStoreAction.CredentialsWrapper credentialsWrapper = domainWrapper.getCredential(credentials.getId());
        if (credentialsWrapper != null) {
            return new CreateResponse(new CredentialApi.Credential(credentialsWrapper, getLink().rel("domains").rel(domainName).rel("credentials")));
        }
    }
    // this should never happen
    throw new ServiceException.UnexpectedErrorException("Unexpected error, failed to create credential");
}
Also used : User(hudson.model.User) IdCredentials(com.cloudbees.plugins.credentials.common.IdCredentials) CreateResponse(io.jenkins.blueocean.rest.model.CreateResponse) JSONObject(net.sf.json.JSONObject) CredentialsStoreAction(com.cloudbees.plugins.credentials.CredentialsStoreAction) WebMethod(org.kohsuke.stapler.WebMethod) POST(org.kohsuke.stapler.verb.POST)

Aggregations

CredentialsStoreAction (com.cloudbees.plugins.credentials.CredentialsStoreAction)1 IdCredentials (com.cloudbees.plugins.credentials.common.IdCredentials)1 User (hudson.model.User)1 CreateResponse (io.jenkins.blueocean.rest.model.CreateResponse)1 JSONObject (net.sf.json.JSONObject)1 WebMethod (org.kohsuke.stapler.WebMethod)1 POST (org.kohsuke.stapler.verb.POST)1