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

     1  package stacks
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  // CreateOpts is the structure used to create a stack.
     8  type CreateOpts struct {
     9  	// The name of the stack.
    10  	// The value can contain 1 to 64 characters, only letters, digits and hyphens (-) are allowed.
    11  	// The name must start with a letter and end with a letter or digit.
    12  	Name string `json:"stack_name" required:"true"`
    13  	// The agencies authorized to IAC.
    14  	Agencies []Agency `json:"agencies,omitempty"`
    15  	// The description of the stack.
    16  	// The value can contain 0 to 1024 characters.
    17  	Description string `json:"description,omitempty"`
    18  	// The flag of the deletion protection.
    19  	// The resource stack deletion protection is not enabled by default (the resource stack is not allowed to be deleted
    20  	// after the deletion protection is enabled).
    21  	EnableDeletionProtection *bool `json:"enable_deletion_protection,omitempty"`
    22  	// The automatic rollback flag.
    23  	// The automatic rollback of the resource stack is not enabled by default (after automatic rollback is enabled, if
    24  	// the deployment fails, it will automatically roll back and return to the previous stable state).
    25  	EnableAutoRollback *bool `json:"enable_auto_rollback,omitempty"`
    26  	// The HCL template content for deployment resources.
    27  	TemplateBody string `json:"template_body,omitempty"`
    28  	// The OBS address where the HCL template ZIP is located, which describes the target status of the deployment
    29  	// resources.
    30  	TemplateUri string `json:"template_uri,omitempty"`
    31  	// The variable content for deployment resources.
    32  	VarsBody string `json:"vars_body,omitempty"`
    33  	// The variable structures for deployment resources.
    34  	VarsStructure []VarsStructure `json:"vars_structure,omitempty"`
    35  	// The OBS address where the variable ZIP corresponding to the HCL template is located, which describes the target
    36  	// status of the deployment resources.
    37  	VarsUri string `json:"vars_uri,omitempty"`
    38  }
    39  
    40  // Agency is an object that represents the IAC agency configuration.
    41  type Agency struct {
    42  	// The name of the provider corresponding to the IAM agency.
    43  	// If the provider_name given by the user contains duplicate values, return 400.
    44  	ProviderName string `json:"provider_name" required:"true"`
    45  	// The name of IAM agency authorized to IAC account.
    46  	// RF will use the agency authority to access and create resources corresponding to the provider.
    47  	AgencyName string `json:"agency_name" required:"true"`
    48  }
    49  
    50  // VarsStructure is an object that represents the variable structure details.
    51  type VarsStructure struct {
    52  	// The variable key.
    53  	VarKey string `json:"var_key" required:"true"`
    54  	// The variable value.
    55  	VarValue string `json:"var_value" required:"true"`
    56  	// The encryption configuration.
    57  	Encryption Encryption `json:"encryption,omitempty"`
    58  }
    59  
    60  // Encryption is an object that represents the KMS usage for variables.
    61  type Encryption struct {
    62  	// Encryption configuration for variables.
    63  	Kms KmsStructure `json:"kms" required:"true"`
    64  }
    65  
    66  // KmsStructure is an object that represents the encrypt structure.
    67  type KmsStructure struct {
    68  	// The ID of the KMD secret key.
    69  	ID string `json:"id" required:"true"`
    70  	// The ciphertext corresponding to the data encryption key.
    71  	CipherText string `json:"cipher_text" required:"true"`
    72  }
    73  
    74  // Create is a method to create a stack using create option.
    75  func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*CreateResp, error) {
    76  	b, err := golangsdk.BuildRequestBody(opts, "")
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	var r CreateResp
    82  	_, err = client.Post(rootURL(client), b, &r, &golangsdk.RequestOpts{
    83  		MoreHeaders: client.MoreHeaders,
    84  	})
    85  	return &r, err
    86  }
    87  
    88  // ListAll is a method to query all stacks and returns a stack list.
    89  func ListAll(client *golangsdk.ServiceClient) ([]Stack, error) {
    90  	var r listResp
    91  	_, err := client.Get(rootURL(client), &r, &golangsdk.RequestOpts{
    92  		MoreHeaders: client.MoreHeaders,
    93  	})
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return r.Stacks, nil
    99  }
   100  
   101  // DeployOpts is the structure used to deploy a terraform script.
   102  type DeployOpts struct {
   103  	// The HCL template content for terraform resources.
   104  	TemplateBody string `json:"template_body,omitempty"`
   105  	// The OBS address where the HCL template ZIP is located, which describes the target status of the terraform
   106  	// resources.
   107  	TemplateUri string `json:"template_uri,omitempty"`
   108  	// The variable content for terraform resources.
   109  	VarsBody string `json:"vars_body,omitempty"`
   110  	// The variable structures for terraform resources.
   111  	VarsStructure []VarsStructure `json:"vars_structure,omitempty"`
   112  	// The OBS address where the variable ZIP corresponding to the HCL template is located, which describes the target
   113  	// status of the deployment resources.
   114  	VarsUri string `json:"vars_uri,omitempty"`
   115  	// The unique ID of the resource stack.
   116  	StackId string `json:"stack_id,omitempty"`
   117  }
   118  
   119  // Deploy is a method to deploy a terraform script using given parameters.
   120  func Deploy(client *golangsdk.ServiceClient, stackName string, opts DeployOpts) (string, error) {
   121  	b, err := golangsdk.BuildRequestBody(opts, "")
   122  	if err != nil {
   123  		return "", err
   124  	}
   125  
   126  	var r deployResp
   127  	_, err = client.Post(deploymentURL(client, stackName), b, &r, &golangsdk.RequestOpts{
   128  		MoreHeaders: client.MoreHeaders,
   129  	})
   130  	return r.DeploymentId, err
   131  }
   132  
   133  // ListEventsOpts allows to filter list data using given parameters.
   134  type ListEventsOpts struct {
   135  	// The unique ID of the resource stack.
   136  	StackId string `q:"stack_id"`
   137  	// The unique ID of the resource deployment.
   138  	DeploymentId string `q:"deployment_id"`
   139  }
   140  
   141  // ListAllEvents is a method to query all events for a specified stack using given parameters.
   142  func ListAllEvents(client *golangsdk.ServiceClient, stackName string, opts ListEventsOpts) ([]StackEvent, error) {
   143  	url := eventURL(client, stackName)
   144  	query, err := golangsdk.BuildQueryString(opts)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	url += query.String()
   149  
   150  	var r listEventsResp
   151  	_, err = client.Get(url, &r, &golangsdk.RequestOpts{
   152  		MoreHeaders: client.MoreHeaders,
   153  	})
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	return r.StackEvents, nil
   159  }
   160  
   161  // DeleteOpts is the structure used to remove a stack.
   162  type DeleteOpts struct {
   163  	// The stack ID.
   164  	StackId string `json:"stack_id,omitempty"`
   165  }
   166  
   167  // Delete is a method to remove a stack using stack name and delete option.
   168  func Delete(client *golangsdk.ServiceClient, stackName string, opts DeleteOpts) error {
   169  	url := resourceURL(client, stackName)
   170  	query, err := golangsdk.BuildQueryString(opts)
   171  	if err != nil {
   172  		return err
   173  	}
   174  	url += query.String()
   175  
   176  	_, err = client.Delete(url, &golangsdk.RequestOpts{
   177  		MoreHeaders: client.MoreHeaders,
   178  	})
   179  	return err
   180  }