Help Center/ Virtual Private Network/ API Reference/ Application Examples/ Example 6: Creating a User and a User Group
Updated on 2024-12-03 GMT+08:00

Example 6: Creating a User and a User Group

Scenario

This section describes how to create a VPN user and a user group by calling APIs.

Prerequisites

  • You have created a P2C VPN gateway.
  • You have created a VPN server and set the authentication mode of the server to password authentication. For details, see Creating a Server.
  • You have determined the endpoint for calling APIs.
  • You have obtained a user token if you need to use token authentication. In addition, you need to add X-Auth-Token to the request header when calling an API. For details about token authentication, see Authentication.

    The token obtained through IAM is valid for only 24 hours. When using a token for authentication, cache it to avoid frequent calling.

    Calling the APIs related to users and user groups is an asynchronous process. The configuration takes effect after a period of time. If you frequently perform operations on users or user groups within a short period of time, the queue may be congested due to limited performance, and the error message "VPN.0030, The system is busy, please try later." is returned. In this case, you need to wait for a while before calling the APIs again.

Data Preparation

Table 1 Key parameter in the request for creating a user group

Parameter

Description

Example Value

vpn_server_id

Specifies the ID of a VPN server.

0e325fb6-83b9-4004-a343-8b6fc714a5d9

Table 2 Key parameter in the request for creating a user

Parameter

Description

Example Value

vpn_server_id

Specifies the ID of a VPN server.

0e325fb6-83b9-4004-a343-8b6fc714a5d9

Procedure

  1. Create a user group.
    1. Send POST https://{endpoint}/v5/{project_id}/p2c-vpn-gateways/vpn-servers/{vpn_server_id}/groups. {vpn_server_id} specifies the ID of the created VPN server.
    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      {
        "user_group": {
          "name": "user-group1",
          "description": "UserGroup1"
        }
      }
    4. Check the response.
      • The request is successful if the following response is displayed. In the response, id specifies the ID of the created user group.
        {
            "user_group": {
                "id": "7625fd92-2e20-4e4d-8c56-66f110fbfaa8"
            },
            "request_id": "94d271493e144135423e7377e40127cf"
        }
  2. Query the user group.
    1. Send GET https://{endpoint}/v5/{project_id}/p2c-vpn-gateways/vpn-servers/{vpn_server_id}/groups/{group_id}. {group_id} specifies the ID of the created user group.
    2. Add X-Auth-Token to the request header.
    3. Check the response.
      {
          "user_group": {
              "id": "7625fd92-2e20-4e4d-8c56-66f110fbfaa8",
              "name": "user-group1",
              "description": "UserGroup1",
              "type": "Custom",
              "user_number": 0,
              "created_at": "2024-06-17T09:48:27.548Z",
              "updated_at": "2024-06-17T09:48:27.548Z"
          },
          "request_id": "6735d32bb3e35e9154caba1dbc6c2dc6"
      }
  3. Create a user.
    1. Send POST https://{endpoint}/v5/{project_id}/p2c-vpn-gateways/vpn-servers/{vpn_server_id}/users. {vpn_server_id} specifies the ID of the created VPN server.
    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      {
        "user": {
          "name": "user001",
          "password": "Qwerasdf",
          "description": "User1",
          "user_group_id": "7625fd92-2e20-4e4d-8c56-66f110fbfaa8"
        }
      }
    4. Check the response.
      • The request is successful if the following response is displayed. In the response, id specifies the ID of the created user.
        {
          "user": {
            "id": "41ad472e-d3e4-482b-8f00-7b2c1bfc4b7d"
          },
          "request_id": "b19ba5a0be8f7b7f664b14596f8f35db"
        }
  4. Query the user.
    1. Send GET https://{endpoint}/v5/{project_id}/p2c-vpn-gateways/vpn-servers/{vpn_server_id}/users/{user_id}. {vpn_server_id} specifies the ID of the created VPN server. {user_id} specifies the ID of the created user.
    2. Add X-Auth-Token to the request header.
    3. Check the response.
      {
          "user": {
              "id": "41ad472e-d3e4-482b-8f00-7b2c1bfc4b7d",
              "name": "user001",
              "description": "User1",
              "user_group_id": "7625fd92-2e20-4e4d-8c56-66f110fbfaa8",
              "user_group_name": "user-group1",
              "created_at": "2024-06-17T06:53:46.302Z",
              "updated_at": "2024-06-17T06:53:46.302Z"
          },
          "request_id": "926a0edb3bf432943e2399b700173add"
      }