You can use the Coverity on Polaris API to create projects, users, and user roles with
the following cURL, Python, and Java API examples.
cURL: Create a New Project, User, and Role
- Get the organization, and extract the organization ID.
$ curl -X GET \ https://subdomain.cop.blackduck.com/api/auth/v1/organizations&page[limit]=1&page[offset]=0 \ -H 'Accept: application/vnd.api+json' \ -H 'Authorization: Bearer {JWT}' - Create a project, and extract the project ID from the response. Project names must be unique within the organization, and are case-insensitive.
$ curl -X POST \ https://subdomain.cop.blackduck.com/api/common/v0/projects \ -H 'Accept: application/vnd.api+json' \ -H 'Authorization: Bearer {JWT}' \ -H 'Content-Type: application/vnd.api+json' \ -d '{"data":{"attributes":{"name":"test project"},"type":"project"}}' - Create a user.
$ curl -X POST \ https://subdomain.cop.blackduck.com/api/auth/v1/users \ -H 'Accept: application/vnd.api+json' \ -H 'Authorization: Bearer {JWT}' \ -H 'Content-Type: application/vnd.api+json' \ -d '{"data":{"type":"users","attributes": \ {"email":"user@example.com","name":"User","username": \ "username","enabled":true}},"relationships":{"organization": \ {"data":{"type":"organizations","id":"{org ID}"}}}}}' - Get the role list and extract the appropriate role ID.
$ curl -X GET \ https://subdomain.cop.blackduck.com/api/auth/v1/roles&page[limit]=10&page[offset]=0 \ -H 'Accept: application/vnd.api+json' \ -H 'Authorization: Bearer {JWT}' - Add a role assignment with the user ID, organization ID, project ID, and role ID.
$ curl -X POST \ https://subdomain.cop.blackduck.com/api/auth/v1/role-assignments \ -H 'Authorization: Bearer {JWT}' \ -H 'Content-Type: application/vnd.api+json' \ -d '{"data":{"type":"role-assignments","attributes":{"object":"urn:x-swip:projects:{{project-id}}"},"relationships":{"role":{"data":{"type":"roles","id":"{{role-id}}"}},"organization":{"data":{"type":"organizations","id":"{{org-id}}"}},"user":{"data":{"type":"users","id":"{{user-id}}"}}}}}'
Python: Create a New Project, User, and Role
import json
from http.client import HTTPSConnection, HTTPResponse
polaris = HTTPSConnection('subdomain.cop.blackduck.com')
jwt = ''
json_api_resource_headers = {
'Content-Type': "application/vnd.api+json",
'Accept': "application/vnd.api+json",
'Authorization': 'Bearer ' + jwt
}
# step 1 get your organization, extract the organization ID
polaris.request("GET", "/api/auth/v1/organizations?page[limit]=1&page[offset]=0", None, json_api_resource_headers)
organizations_response = polaris.getresponse()
print(f'Organizations: \n{json.dumps(organizations_response.read(), indent=4)}')
organization_id = ''
# step 2 create a project, extract the project ID from the response
post_project_body = {
"data": {
"attributes": {
"name": "test project"
},
"type": "project"
}
}
polaris.request("POST", "/api/common/v0/projects", json.dumps(post_project_body), json_api_resource_headers)
projects_response = polaris.getresponse()
print(f'Project: \n{json.dumps(projects_response.read(), indent=4)}')
project_id ''
# step 3 create a user
post_user_body = {
"data": {
"type": "users",
"attributes": {
"email": "user@example.com",
"name": "name",
"username": "username",
"enabled": True
},
"relationships": {
"organization": {
"data": {
"type": "organizations",
"id": f"{organization_id}"
}
}
}
}
}
polaris.request("POST", "/api/auth/v1/users", json.dumps(post_user_body), json_api_resource_headers)
users_response = polaris.getresponse()
print(f'New user: \n{json.dumps(users_response.read(), indent=4)}')
user_id = ''
# step 4 get the role list, extract the appropriate role ID
polaris.request("GET", "/api/auth/v1/roles?page[limit]=3&page[offset]=0", None, json_api_resource_headers)
roles_response = polaris.getresponse()
print(f'Roles: \n{json.dumps(roles_response.read(), indent=4)}')
role_id = ''
# step 5 add a role assignment with the user id, organization id, project id and role id
post_role_assignment_body = {
"data": {
"type": "role-assignments",
"attributes": {
"object": f"urn:x-swip:projects:{project_id}"
},
"relationships": {
"role": {
"data": {
"type": "roles",
"id": f"{role_id}"
}
},
"organization": {
"data": {
"type": "organizations",
"id": f"{organization_id}"
}
},
"user": {
"data": {
"type": "users",
"id": f"{user_id}"
}
}
}
}
}
polaris.request("POST", "/api/auth/v1/role-assignments", json.dumps(post_role_assignment_body), json_api_resource_headers)
role_assignments_response = polaris.getresponse()
print(f'New role assignment: \n{json.dumps(role_assignments_response.read(), indent=4)}')Java: Create a New Project, User, and Role
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
final static String JSON_API_STRING = "application/vnd.api+json";
String jwt = "";
String host = "https://subdomain.cop.blackduck.com";
OkHttpClient client = new OkHttpClient();
// step 1 get your organization, extract the organization ID
Request getOrganizationRequest = new Request.Builder()
.url(host+"/api/auth/v1/organizations?page[limit]=1&page[offset]=0")
.get()
.addHeader("Accept", JSON_API_STRING)
.addHeader("Authorization", "Bearer "+jwt)
.build();
Response getOrganizationResponse = client.newCall(getOrganizationRequest).execute();
String organizationId = "";
// step 2 create a project, extract the project ID from the response
MediaType jsonApiMediaType = MediaType.parse(JSON_API_STRING);
RequestBody createProjectBody = RequestBody.create(jsonApiMediaType, "{\"data\": {\"attributes\": {\"name\": \"test project\"},\"type\": \"project\"}}");
Request createProjectRequest = new Request.Builder()
.url(host+"/api/common/v0/projects")
.post(createProjectBody)
.addHeader("Content-Type", JSON_API_STRING)
.addHeader("Accept", JSON_API_STRING)
.addHeader("Authorization", "Bearer "+jwt)
.build();
Response createProjectResponse = client.newCall(createProjectRequest).execute();
String projectId = "";
// step 3 create a user
RequestBody createUserBody = RequestBody.create(jsonApiMediaType, "{\"data\": {\"type\": \"users\",\"attributes\": {\"email\": \"user@example.com\",\"name\": \"name\",\"username\": \"username\",\"enabled\": true},\"relationships\": {\"organization\": {\"data\": {\"type\": \"organizations\",\"id\": \""+organizationId+"\"}}}},\"included\": []}");
Request createUserRequest = new Request.Builder()
.url(host+"/api/auth/v1/users/")
.post(createUserBody)
.addHeader("Content-Type", JSON_API_STRING)
.addHeader("Accept", JSON_API_STRING)
.addHeader("Authorization", "Bearer "+jwt)
.build();
Response createUserResponse = client.newCall(createUserRequest).execute();
String userId = "";
// step 4 get the role list, extract the appropriate role ID
Request getRoleRequest = new Request.Builder()
.url(host+"/api/auth/v1/roles?page[limit]=1&page[offset]=0")
.get()
.addHeader("Accept", JSON_API_STRING)
.addHeader("Authorization", "Bearer "+jwt)
.build();
Response getRoleResponse = client.newCall(getRoleRequest).execute();
String roleId = "";
// step 5 add a role assignment with the user id, organization id, project id and role id
RequestBody createRoleAssignmentBody = RequestBody.create(jsonApiMediaType, "{\"data\": {\"type\": \"role-assignments\",\"attributes\": {\"object\": \"urn:x-swip:projects:"+projectId+"\"},\"relationships\": {\"role\": {\"data\": {\"type\": \"roles\",\"id\": \""+roleId+"\"}},\"organization\": {\"data\": {\"type\": \"organizations\",\"id\": \""+organizationId+"\"}},\"user\": {\"data\": {\"type\": \"users\",\"id\": \""+userId+"\"}}}}}");
Request createRoleAssignmentRequest = new Request.Builder()
.url(host+"/api/auth/v1/users/")
.post(createRoleAssignmentBody)
.addHeader("Content-Type", JSON_API_STRING)
.addHeader("Accept", JSON_API_STRING)
.addHeader("Authorization", "Bearer "+jwt)
.build();
Response createRoleAssignmentResponse = client.newCall(createRoleAssignmentRequest).execute();