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

     1  package connections
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure that used by 'Create' method to create a new data connection.
     9  type CreateOpts struct {
    10  	// The ID of the workspace where the data connection is located.
    11  	WorkspaceId string `json:"-" required:"true"`
    12  	// The list of structure for data source configuration.
    13  	DataSourceVos []DataSourceVo `json:"data_source_vos" required:"true"`
    14  }
    15  
    16  // DataSourceVo is the structure that represents the configuration of the data source.
    17  type DataSourceVo struct {
    18  	// The data connection name.
    19  	DwName string `json:"dw_name" required:"true"`
    20  	// The data connection type.
    21  	DwType string `json:"dw_type" required:"true"`
    22  	// The dynamic configuration for the specified type of data source.
    23  	DwConfig interface{} `json:"dw_config" required:"true"`
    24  	// The agent ID.
    25  	AgentId string `json:"agent_id,omitempty"`
    26  	// The agent name.
    27  	AgentName string `json:"agent_name,omitempty"`
    28  	// The data connection mode.
    29  	EnvType int `json:"env_type,omitempty"`
    30  }
    31  
    32  func buildRequestOpts(workspaceId string) *golangsdk.RequestOpts {
    33  	return &golangsdk.RequestOpts{
    34  		MoreHeaders: map[string]string{
    35  			"Content-Type": "application/json",
    36  			"X-Language":   "en-us",
    37  			"workspace":    workspaceId, // workspace is required.
    38  		},
    39  	}
    40  }
    41  
    42  // Create is a method that used to create a new connection using given parameters.
    43  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (string, error) {
    44  	b, err := golangsdk.BuildRequestBody(opts, "")
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  
    49  	var r createResp
    50  	_, err = c.Post(rootURL(c), b, &r, buildRequestOpts(opts.WorkspaceId))
    51  	return r.DataConnectionId, err
    52  }
    53  
    54  // ValidateOpts is the structure that represents the pre-check configuration.
    55  type ValidateOpts struct {
    56  	// The ID of the workspace where the data connection is located.
    57  	WorkspaceId string `json:"-" required:"true"`
    58  	// The data connection name.
    59  	DwName string `json:"dw_name" required:"true"`
    60  	// The data connection type.
    61  	DwType string `json:"dw_type" required:"true"`
    62  	// The dynamic configuration for the specified type of data source.
    63  	DwConfig interface{} `json:"dw_config" required:"true"`
    64  	// The agent ID.
    65  	AgentId string `json:"agent_id,omitempty"`
    66  	// The agent name.
    67  	AgentName string `json:"agent_name,omitempty"`
    68  	// The data connection mode.
    69  	EnvType int `json:"env_type,omitempty"`
    70  }
    71  
    72  // Validate is a method that used to pre-check the configuration of the data connection using given parameters.
    73  func Validate(c *golangsdk.ServiceClient, opts ValidateOpts) (*ValidateResp, error) {
    74  	b, err := golangsdk.BuildRequestBody(opts, "")
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	var r ValidateResp
    80  	_, err = c.Post(validateURL(c), b, &r, buildRequestOpts(opts.WorkspaceId))
    81  	return &r, err
    82  }
    83  
    84  // Get is a method to obtain the data connection detail by its ID and related workspace ID.
    85  func Get(c *golangsdk.ServiceClient, workspaceId, connectionId string) (*Connection, error) {
    86  	var r Connection
    87  	_, err := c.Get(resourceURL(c, connectionId), &r, buildRequestOpts(workspaceId))
    88  	return &r, err
    89  }
    90  
    91  // ListOpts is the structure used by 'List' method to query data connections.
    92  type ListOpts struct {
    93  	// The ID of the workspace where the data connection is located.
    94  	WorkspaceId string `json:"-" required:"true"`
    95  	// The data connection name.
    96  	Name string `q:"name"`
    97  	// The data connection type.
    98  	Type string `q:"type"`
    99  	// Limit is the records count to be queried.
   100  	Limit string `q:"limit"`
   101  	// Offset number.
   102  	Offset string `q:"offset"`
   103  }
   104  
   105  // List is a method to query the list of the data connections using given parameters.
   106  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Connection, error) {
   107  	url := rootURL(c)
   108  	query, err := golangsdk.BuildQueryString(opts)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	url += query.String()
   113  
   114  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   115  		return ConnectionPage{pagination.OffsetPageBase{PageResult: r}}
   116  	})
   117  	queryOpts := buildRequestOpts(opts.WorkspaceId)
   118  	pager.Headers = queryOpts.MoreHeaders
   119  	pages, err := pager.AllPages()
   120  
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	return extractConnections(pages)
   125  }
   126  
   127  // UpdateOpts is the structure that used by 'Update' method to modify the configuration of the data connection.
   128  type UpdateOpts struct {
   129  	// The connection ID.
   130  	ConnectionId string `json:"-" required:"true"`
   131  	// The ID of the workspace where the data connection is located.
   132  	WorkspaceId string `json:"-" required:"true"`
   133  	// The list of structure for data source configuration.
   134  	DataSourceVos []DataSourceVo `json:"data_source_vos" required:"true"`
   135  }
   136  
   137  // Update is a method to modify the specified connection using given parameters.
   138  func Update(c *golangsdk.ServiceClient, opts UpdateOpts) error {
   139  	b, err := golangsdk.BuildRequestBody(opts, "")
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	_, err = c.Put(resourceURL(c, opts.ConnectionId), b, nil, buildRequestOpts(opts.WorkspaceId))
   145  	return err
   146  }
   147  
   148  // Delete is a method to delete a specified connection by its ID and the related workspace ID.
   149  func Delete(client *golangsdk.ServiceClient, workspaceId, connnectionId string) error {
   150  	_, err := client.Delete(resourceURL(client, connnectionId), buildRequestOpts(workspaceId))
   151  	return err
   152  }