github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/workspace/v2/policygroups/requests.go (about)

     1  package policygroups
     2  
     3  import "github.com/chnsz/golangsdk"
     4  
     5  // CreateOpts is the structure that represents the policy group configuration.
     6  type CreateOpts struct {
     7  	// Policy group name.
     8  	PolicyGroupName string `json:"policy_group_name" required:"true"`
     9  	// Policy group ID.
    10  	PolicyGroupId string `json:"policy_group_id,omitempty"`
    11  	// Priority.
    12  	Priority int `json:"priority,omitempty"`
    13  	// Policy group description.
    14  	Description string `json:"description,omitempty"`
    15  	// The update time of the policy group.
    16  	UpdateTime string `json:"update_time,omitempty"`
    17  	// List of target objects.
    18  	Targets []Target `json:"targets,omitempty"`
    19  	// Access policy configuration.
    20  	Policies Policy `json:"policies,omitempty"`
    21  }
    22  
    23  // Target is the structure that represents the access target configuration.
    24  type Target struct {
    25  	// Target ID.
    26  	// If the target type is 'INSTANCE', the ID means the SID of the desktop.
    27  	// If the target type is 'USER', the ID means the user ID.
    28  	// If the target type is 'USERGROUP', the ID means the user group ID.
    29  	// If the target type is 'CLIENTIP', the ID means the terminal IP address.
    30  	// If the target type is 'OU', the ID means the OUID.
    31  	// If the target type is 'ALL', the ID fixed with string 'default-apply-all-targets'.
    32  	TargetId string `json:"target_id,omitempty"`
    33  	// Target name.
    34  	// If the target type is 'INSTANCE', the ID means the desktop name.
    35  	// If the target type is 'USER', the ID means the user name.
    36  	// If the target type is 'USERGROUP', the ID means the user group name.
    37  	// If the target type is 'CLIENTIP', the ID means the terminal IP address.
    38  	// If the target type is 'OU', the ID means the OU name.
    39  	// If the target type is 'ALL', the ID fixed with string 'All-Targets'.
    40  	TargetName string `json:"target_name,omitempty"`
    41  	// Target type.
    42  	// + INSTANCE: Desktop.
    43  	// + USER: User.
    44  	// + USERGROUP: User group.
    45  	// + CLIENTIP: Terminal IP address.
    46  	// + OU: Organization unit.
    47  	// + ALL: All desktops.
    48  	TargetType string `json:"target_type,omitempty"`
    49  }
    50  
    51  // Policy is the structure that represents the access policy detail.
    52  type Policy struct {
    53  	// Access control.
    54  	AccessControl AccessControl `json:"access_control,omitempty"`
    55  }
    56  
    57  // AccessControl is the structure that represents the access policy detail.
    58  type AccessControl struct {
    59  	// IP access control.
    60  	// It consists of multiple groups of IP addresses and network masks, separated by ';',
    61  	// and spliced together by '|' between IP addresses and network masks.
    62  	// Such as: "IP|mask;IP|mask;IP|mask"
    63  	IpAccessControl string `json:"ip_access_control,omitempty"`
    64  }
    65  
    66  var requestOpts = golangsdk.RequestOpts{
    67  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    68  }
    69  
    70  // Create is a method to create a policy group using given parameters.
    71  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (string, error) {
    72  	b, err := golangsdk.BuildRequestBody(opts, "policy_group")
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  
    77  	var r struct {
    78  		ID string `json:"id"`
    79  	}
    80  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
    81  		MoreHeaders: requestOpts.MoreHeaders,
    82  	})
    83  	return r.ID, err
    84  }
    85  
    86  // Get is a method to obtain the policy group detail by its ID.
    87  func Get(c *golangsdk.ServiceClient, groupId string) (*PolicyGroup, error) {
    88  	var r struct {
    89  		PolicyGroup PolicyGroup `json:"policy_group"`
    90  	}
    91  	_, err := c.Get(resourceURL(c, groupId), &r, &golangsdk.RequestOpts{
    92  		MoreHeaders: requestOpts.MoreHeaders,
    93  	})
    94  	return &r.PolicyGroup, err
    95  }
    96  
    97  // UpdateOpts is the structure that used to modify the policy group configuration.
    98  type UpdateOpts struct {
    99  	// Policy group ID.
   100  	PolicyGroupId string `json:"-" required:"true"`
   101  	// Policy group name.
   102  	PolicyGroupName string `json:"policy_group_name" required:"true"`
   103  	// Priority.
   104  	Priority int `json:"priority,omitempty"`
   105  	// Policy group description.
   106  	Description *string `json:"description,omitempty"`
   107  	// List of target objects.
   108  	Targets []Target `json:"targets,omitempty"`
   109  	// List of policy.
   110  	Policies *Policy `json:"policies,omitempty"`
   111  }
   112  
   113  // Update is a method to modify the existing policy group configuration using given parameters.
   114  func Update(c *golangsdk.ServiceClient, opts UpdateOpts) (string, error) {
   115  	b, err := golangsdk.BuildRequestBody(opts, "policy_group")
   116  	if err != nil {
   117  		return "", err
   118  	}
   119  
   120  	var r struct {
   121  		ID string `json:"id"`
   122  	}
   123  	_, err = c.Put(resourceURL(c, opts.PolicyGroupId), b, &r, &golangsdk.RequestOpts{
   124  		MoreHeaders: requestOpts.MoreHeaders,
   125  	})
   126  	return r.ID, err
   127  }
   128  
   129  // Delete is a method to remove an existing policy group using given parameters.
   130  func Delete(c *golangsdk.ServiceClient, groupId string) error {
   131  	_, err := c.Delete(resourceURL(c, groupId), &golangsdk.RequestOpts{
   132  		MoreHeaders: requestOpts.MoreHeaders,
   133  	})
   134  	return err
   135  }