Creating an Access Policy
Function
This API is used to create an access policy.
Calling Method
For details, see Calling APIs.
URI
POST /api/v3/access-policies
Request Parameters
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
kind |
Yes |
String |
Definition: API type Constraints: The value cannot be changed. Range: N/A Default Value: AccessPolicy |
|
apiVersion |
Yes |
String |
Definition: API version Constraints: The value cannot be changed. Range: N/A Default Value: v3 |
|
name |
Yes |
String |
Definition Access policy name. Constraints Enter 1 to 56 characters starting with a lowercase letter and not ending with a hyphen (-). Only lowercase letters, digits, and hyphens (-) are allowed. Range N/A Default Value N/A |
|
clusters |
Yes |
Array of strings |
Definition: List of cluster IDs. Wildcards (*) are allowed, which indicate all clusters. For details about how to obtain the value, see How to Obtain Parameters in the API URI. Constraints: A maximum of 200 clusters can be authorized at a time. Range: [*] or cluster ID list Default Value: N/A |
|
accessScope |
Yes |
AccessScope object |
Definition: Access scope, which is used to specify the cluster and namespace to be authorized Constraints: N/A |
|
policyType |
Yes |
String |
Definition Permission type. Constraints N/A Range
Default Value N/A |
|
principal |
Yes |
Principal object |
Definition: Authorization object Constraints: N/A |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
namespaces |
Yes |
Array of strings |
Definition List of cluster namespaces. Wildcards (*) are allowed to indicate all namespaces. If different clusters are selected, the namespace list can be a collection of multiple clusters. When RBAC authorization is used, CCE automatically checks whether the namespaces exist in the clusters. Constraints A maximum of 500 namespaces can be authorized at a time. Range [*] or cluster namespace list Default Value N/A |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
type |
Yes |
String |
Definition: Type of the authorization object Constraints: N/A Range:
Default Value: N/A |
|
ids |
Yes |
Array of strings |
Definition: List of IDs of authorized objects. Enter the IDs based on the object type, user, user group, and agency account. Constraints: A maximum of 500 users or user groups can be authorized at a time. Range: N/A Default Value: N/A |
Response Parameters
Status code: 201
|
Parameter |
Type |
Description |
|---|---|---|
|
kind |
String |
Definition: API type Constraints: The value cannot be changed. Range: N/A Default Value: AccessPolicy |
|
apiVersion |
String |
Definition: API version Constraints: The value cannot be changed. Range: N/A Default Value: v3 |
|
name |
String |
Definition Access policy name. Constraints Enter 1 to 56 characters starting with a lowercase letter and not ending with a hyphen (-). Only lowercase letters, digits, and hyphens (-) are allowed. Range N/A Default Value N/A |
|
policyId |
String |
Definition: Permission ID Constraints: The value is automatically generated and cannot be changed. Range: N/A Default Value: N/A |
|
clusters |
Array of strings |
Definition: List of cluster IDs. Wildcards (*) are allowed, which indicate all clusters. Constraints: A maximum of 200 clusters can be authorized at a time. Range: [*] or cluster ID list Default Value: N/A |
|
accessScope |
AccessScope object |
Definition: Access scope, which is used to specify the cluster and namespace to be authorized Constraints: N/A |
|
policyType |
String |
Definition Permission type Constraints N/A Range
Default Value N/A |
|
principal |
Principal object |
Definition: Authorization object Constraints: N/A |
|
createTime |
String |
Definition: Creation time Constraints: N/A Range: N/A Default Value: N/A |
|
updateTime |
String |
Definition: Update time Constraints: N/A Range: N/A Default Value: N/A |
|
Parameter |
Type |
Description |
|---|---|---|
|
namespaces |
Array of strings |
Definition List of cluster namespaces. Wildcards (*) are allowed to indicate all namespaces. If different clusters are selected, the namespace list can be a collection of multiple clusters. When RBAC authorization is used, CCE automatically checks whether the namespaces exist in the clusters. Constraints A maximum of 500 namespaces can be authorized at a time. Range [*] or cluster namespace list Default Value N/A |
|
Parameter |
Type |
Description |
|---|---|---|
|
type |
String |
Definition: Type of the authorization object Constraints: N/A Range:
Default Value: N/A |
|
ids |
Array of strings |
Definition: List of IDs of authorized objects. Enter the IDs based on the object type, user, user group, and agency account. Constraints: A maximum of 500 users or user groups can be authorized at a time. Range: N/A Default Value: N/A |
Example Requests
Create an access policy.
POST /api/v3/access-policies
{
"kind" : "AccessPolicy",
"apiVersion" : "v3",
"name" : "test-access-policy",
"clusters" : [ "*" ],
"accessScope" : {
"namespaces" : [ "*" ]
},
"policyType" : "CCEAdminPolicy",
"principal" : {
"type" : "user",
"ids" : [ "069fcc2116c347b89869eae3cdxxxxxx1", "069fcc2116c347b89869eae3cdxxxxxx2" ]
}
}
Example Responses
Status code: 201
{
"kind" : "AccessPolicy",
"apiVersion" : "v3",
"name" : "test-access-policy",
"policyId" : "402358e8-2e3a-4531-bae7-fe9cba14d0d1",
"clusters" : [ "*" ],
"accessScope" : {
"namespaces" : [ "*" ]
},
"policyType" : "CCEAdminPolicy",
"principal" : {
"type" : "user",
"ids" : [ "069fcc2116c347b89869eae3cdxxxxxx1", "069fcc2116c347b89869eae3cdxxxxxx2" ]
},
"createTime" : "",
"updateTime" : ""
}
SDK Sample Code
The SDK sample code is as follows.
Create an access policy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.cce.v3.region.CceRegion; import com.huaweicloud.sdk.cce.v3.*; import com.huaweicloud.sdk.cce.v3.model.*; import java.util.List; import java.util.ArrayList; public class CreateAccessPolicySolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); CceClient client = CceClient.newBuilder() .withCredential(auth) .withRegion(CceRegion.valueOf("<YOUR REGION>")) .build(); CreateAccessPolicyRequest request = new CreateAccessPolicyRequest(); AccessPolicy body = new AccessPolicy(); List<String> listPrincipalIds = new ArrayList<>(); listPrincipalIds.add("069fcc2116c347b89869eae3cdxxxxxx1"); listPrincipalIds.add("069fcc2116c347b89869eae3cdxxxxxx2"); Principal principalbody = new Principal(); principalbody.withType(Principal.TypeEnum.fromValue("user")) .withIds(listPrincipalIds); List<String> listAccessScopeNamespaces = new ArrayList<>(); listAccessScopeNamespaces.add("*"); AccessScope accessScopebody = new AccessScope(); accessScopebody.withNamespaces(listAccessScopeNamespaces); List<String> listbodyClusters = new ArrayList<>(); listbodyClusters.add("*"); body.withPrincipal(principalbody); body.withPolicyType("CCEAdminPolicy"); body.withAccessScope(accessScopebody); body.withClusters(listbodyClusters); body.withName("test-access-policy"); body.withApiVersion("v3"); body.withKind("AccessPolicy"); request.withBody(body); try { CreateAccessPolicyResponse response = client.createAccessPolicy(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } } |
Create an access policy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcce.v3.region.cce_region import CceRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkcce.v3 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] credentials = BasicCredentials(ak, sk) client = CceClient.new_builder() \ .with_credentials(credentials) \ .with_region(CceRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateAccessPolicyRequest() listIdsPrincipal = [ "069fcc2116c347b89869eae3cdxxxxxx1", "069fcc2116c347b89869eae3cdxxxxxx2" ] principalbody = Principal( type="user", ids=listIdsPrincipal ) listNamespacesAccessScope = [ "*" ] accessScopebody = AccessScope( namespaces=listNamespacesAccessScope ) listClustersbody = [ "*" ] request.body = AccessPolicy( principal=principalbody, policy_type="CCEAdminPolicy", access_scope=accessScopebody, clusters=listClustersbody, name="test-access-policy", api_version="v3", kind="AccessPolicy" ) response = client.create_access_policy(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) |
Create an access policy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := cce.NewCceClient( cce.CceClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateAccessPolicyRequest{} var listIdsPrincipal = []string{ "069fcc2116c347b89869eae3cdxxxxxx1", "069fcc2116c347b89869eae3cdxxxxxx2", } principalbody := &model.Principal{ Type: model.GetPrincipalTypeEnum().USER, Ids: listIdsPrincipal, } var listNamespacesAccessScope = []string{ "*", } accessScopebody := &model.AccessScope{ Namespaces: listNamespacesAccessScope, } var listClustersbody = []string{ "*", } nameAccessPolicy:= "test-access-policy" apiVersionAccessPolicy:= "v3" kindAccessPolicy:= "AccessPolicy" request.Body = &model.AccessPolicy{ Principal: principalbody, PolicyType: "CCEAdminPolicy", AccessScope: accessScopebody, Clusters: listClustersbody, Name: &nameAccessPolicy, ApiVersion: &apiVersionAccessPolicy, Kind: &kindAccessPolicy, } response, err := client.CreateAccessPolicy(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } } |
For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.
Status Codes
|
Status Code |
Description |
|---|---|
|
201 |
Error Codes
See Error Codes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot