github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/rms/v1/policyassignments/requests.go (about)

     1  package policyassignments
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure required by the 'Create' method to assign a specified policy.
     9  type CreateOpts struct {
    10  	// The name of the policy assignment.
    11  	Name string `json:"name" required:"true"`
    12  	// The description of the policy assignment.
    13  	Description string `json:"description,omitempty"`
    14  	// The type of the policy assignment.
    15  	// The valid values are as follow:
    16  	// + builtin
    17  	// + custom
    18  	Type string `json:"policy_assignment_type,omitempty"`
    19  	// The period of the policy rule check.
    20  	Period string `json:"period,omitempty"`
    21  	// The configuration used to filter resources.
    22  	PolicyFilter PolicyFilter `json:"policy_filter,omitempty"`
    23  	// The ID of the policy definition.
    24  	PolicyDefinitionId string `json:"policy_definition_id,omitempty"`
    25  	// The configuration of the custom policy.
    26  	CustomPolicy *CustomPolicy `json:"custom_policy,omitempty"`
    27  	// The rule definition of the policy assignment.
    28  	Parameters map[string]PolicyParameterValue `json:"parameters,omitempty"`
    29  }
    30  
    31  // PolicyFilter is an object specifying the filter parameters.
    32  type PolicyFilter struct {
    33  	// The name of the region to which the filtered resources belong.
    34  	RegionId string `json:"region_id,omitempty"`
    35  	// The service name to which the filtered resources belong.
    36  	ResourceProvider string `json:"resource_provider,omitempty"`
    37  	// The resource type of the filtered resources.
    38  	ResourceType string `json:"resource_type,omitempty"`
    39  	// The ID used to filter resources.
    40  	ResourceId string `json:"resource_id,omitempty"`
    41  	// The tag name used to filter resources.
    42  	TagKey string `json:"tag_key,omitempty"`
    43  	// The tag value used to filter resources.
    44  	TagValue string `json:"tag_value,omitempty"`
    45  }
    46  
    47  // CustomPolicy is an object specifying the configuration of the custom policy.
    48  type CustomPolicy struct {
    49  	// The function URN used to create the custom policy.
    50  	FunctionUrn string `json:"function_urn" required:"true"`
    51  	// The authorization type of the custom policy.
    52  	AuthType string `json:"auth_type" required:"true"`
    53  	// The authorization value of the custom policy.
    54  	AuthValue map[string]interface{} `json:"auth_value,omitempty"`
    55  }
    56  
    57  // PolicyParameterValue is an object specifying the definition of the policy parameter value.
    58  type PolicyParameterValue struct {
    59  	// The value of the rule definition.
    60  	Value interface{} `json:"value,omitempty"`
    61  }
    62  
    63  var requestOpts = golangsdk.RequestOpts{
    64  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    65  }
    66  
    67  // Create is a method to assign a policy using given parameters.
    68  func Create(c *golangsdk.ServiceClient, domainId string, opts CreateOpts) (*Assignment, error) {
    69  	b, err := golangsdk.BuildRequestBody(opts, "")
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	var r Assignment
    75  	_, err = c.Put(rootURL(c, domainId), b, &r, &golangsdk.RequestOpts{
    76  		MoreHeaders: requestOpts.MoreHeaders,
    77  	})
    78  	return &r, err
    79  }
    80  
    81  // Get is a method to obtain the assignment detail by its ID.
    82  func Get(c *golangsdk.ServiceClient, domainId, assignmentId string) (*Assignment, error) {
    83  	var r Assignment
    84  	_, err := c.Get(resourceURL(c, domainId, assignmentId), &r, &golangsdk.RequestOpts{
    85  		MoreHeaders: requestOpts.MoreHeaders,
    86  	})
    87  	return &r, err
    88  }
    89  
    90  // UpdateOpts is the structure required by the Update method to update the assignment configuration.
    91  type UpdateOpts struct {
    92  	// The name of the policy assignment.
    93  	Name string `json:"name" required:"true"`
    94  	// The description of the policy assignment.
    95  	Description *string `json:"description,omitempty"`
    96  	// The type of the policy assignment.
    97  	// The valid values are as follow:
    98  	// + builtin
    99  	// + custom
   100  	Type string `json:"policy_assignment_type,omitempty"`
   101  	// The period of the policy rule check.
   102  	Period string `json:"period,omitempty"`
   103  	// The configuration used to filter resources.
   104  	PolicyFilter PolicyFilter `json:"policy_filter,omitempty"`
   105  	// The ID of the policy definition.
   106  	PolicyDefinitionId string `json:"policy_definition_id,omitempty"`
   107  	// The configuration of the custom policy.
   108  	CustomPolicy *CustomPolicy `json:"custom_policy,omitempty"`
   109  	// The rule definition of the policy assignment.
   110  	Parameters map[string]PolicyParameterValue `json:"parameters,omitempty"`
   111  }
   112  
   113  // Update is a method to update the assignment configuration.
   114  func Update(c *golangsdk.ServiceClient, domainId, assignmentId string, opts UpdateOpts) (*Assignment, error) {
   115  	b, err := golangsdk.BuildRequestBody(opts, "")
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	var r Assignment
   121  	_, err = c.Put(resourceURL(c, domainId, assignmentId), b, &r, &golangsdk.RequestOpts{
   122  		MoreHeaders: requestOpts.MoreHeaders,
   123  	})
   124  	return &r, err
   125  }
   126  
   127  // Enable is a method to enable the resource function of the policy assignment.
   128  func Enable(c *golangsdk.ServiceClient, domainId, assignmentId string) error {
   129  	_, err := c.Post(enableURL(c, domainId, assignmentId), nil, nil, &golangsdk.RequestOpts{
   130  		MoreHeaders: requestOpts.MoreHeaders,
   131  	})
   132  	return err
   133  }
   134  
   135  // Disable is a method to disable the resource function of the policy assignment.
   136  func Disable(c *golangsdk.ServiceClient, domainId, assignmentId string) error {
   137  	_, err := c.Post(disableURL(c, domainId, assignmentId), nil, nil, &golangsdk.RequestOpts{
   138  		MoreHeaders: requestOpts.MoreHeaders,
   139  	})
   140  	return err
   141  }
   142  
   143  // Delete is a method to remove the specified policy assignment using its ID.
   144  func Delete(c *golangsdk.ServiceClient, domainId, assignmentId string) error {
   145  	_, err := c.Delete(resourceURL(c, domainId, assignmentId), &golangsdk.RequestOpts{
   146  		MoreHeaders: requestOpts.MoreHeaders,
   147  	})
   148  	return err
   149  }
   150  
   151  // ListDefinitions is a method to query the list of the built-in policy definitions using given parameters.
   152  func ListDefinitions(client *golangsdk.ServiceClient) ([]PolicyDefinition, error) {
   153  	pages, err := pagination.NewPager(client, queryDefinitionURL(client), func(r pagination.PageResult) pagination.Page {
   154  		p := DefinitionPage{pagination.MarkerPageBase{PageResult: r}}
   155  		p.MarkerPageBase.Owner = p
   156  		return p
   157  	}).AllPages()
   158  
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	return extractDefinitions(pages)
   163  }