common

package
v1.73.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2025 License: MPL-2.0 Imports: 25 Imported by: 18

Documentation

Overview

Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights resvered.

The common package defines some common functions, which are mainly used for the functions of the following services.

The difference between common package and utils:

  1. Common functions under common are related to the project, and common functions are placed here.
  2. Utils are some stored tool functions, which are not related to the project. Such as: date conversion, type conversion.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaseInsensitiveFunc added in v1.31.1

func CaseInsensitiveFunc() schema.SchemaDiffSuppressFunc

func CheckDeleted

func CheckDeleted(d *schema.ResourceData, err error, msg string) error

CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so, sets the resource ID to the empty string instead of throwing an error.

func CheckDeletedDiag added in v1.28.1

func CheckDeletedDiag(d *schema.ResourceData, err error, msg string) diag.Diagnostics

CheckDeletedDiag checks the error to see if it's a 404 (Not Found) and, if so, sets the resource ID to the empty string instead of throwing an error.

func CheckForRetryableError added in v1.29.0

func CheckForRetryableError(err error) *resource.RetryError

func ConvertExpected400ErrInto404Err added in v1.66.1

func ConvertExpected400ErrInto404Err(err error, errCodeKey string, specErrCodes ...string) error

ConvertExpected400ErrInto404Err is a method used to parsing 400 error and try to convert it to 404 error according to the right error code. Arguments: + err: The error response obtained through HTTP/HTTPS request. + errCodeKey: The key name of the error code in the error response body, e.g. 'error_code', 'err_code'. + specErrCodes: One or more error codes that you wish to match against the current error, e.g. 'APIGW.0001'. Notes: If you missing specErrCodes input, this function will convert all 400 errors into 404 errors. How to use it: + For the general cases, their error code key is 'error_code', and we should call as follow:

  • utils.ConvertExpected400ErrInto404Err(err, "error_code")
  • utils.ConvertExpected400ErrInto404Err(err, "error_code", "DLM.3027")
  • utils.ConvertExpected400ErrInto404Err(err, "error_code", []string{"DLM.3027", "DLM.3028"}...)

func ConvertExpected401ErrInto404Err added in v1.68.0

func ConvertExpected401ErrInto404Err(err error, errCodeKey string, specErrCodes ...string) error

ConvertExpected401ErrInto404Err is a method used to parsing 401 error and try to convert it to 404 error according to the right error code. Arguments: + err: The error response obtained through HTTP/HTTPS request. + errCodeKey: The key name of the error code in the error response body, e.g. 'error_code', 'err_code'. + specErrCodes: One or more error codes that you wish to match against the current error, e.g. 'FSS.0401'. Notes: If you missing specErrCodes input, this function will convert all 401 errors into 404 errors. How to use it: + For the general cases, their error code key is 'error_code', and we should call as follow:

  • utils.ConvertExpected401ErrInto404Err(err, "error_code")
  • utils.ConvertExpected401ErrInto404Err(err, "error_code", "FSS.0401")
  • utils.ConvertExpected401ErrInto404Err(err, "error_code", []string{"FSS.0401", "FSS.0402"}...)

func ConvertExpected403ErrInto404Err added in v1.67.1

func ConvertExpected403ErrInto404Err(err error, errCodeKey string, specErrCodes ...string) error

ConvertExpected403ErrInto404Err is a method used to parsing 403 error and try to convert it to 404 error according to the right error code. Arguments: + err: The error response obtained through HTTP/HTTPS request. + errCodeKey: The key name of the error code in the error response body, e.g. 'error_code', 'err_code'. + specErrCodes: One or more error codes that you wish to match against the current error, e.g. 'APIGW.0001'. Notes: If you missing specErrCodes input, this function will convert all 403 errors into 404 errors. How to use it: + For the general cases, their error code key is 'error_code', and we should call as follow:

  • utils.ConvertExpected403ErrInto404Err(err, "error_code")
  • utils.ConvertExpected403ErrInto404Err(err, "error_code", "DWS.0001")
  • utils.ConvertExpected403ErrInto404Err(err, "error_code", []string{"DWS.0001", "DLM.3028"}...)

func ConvertExpected500ErrInto404Err added in v1.67.1

func ConvertExpected500ErrInto404Err(err error, errCodeKey string, specErrCodes ...string) error

ConvertExpected500ErrInto404Err is a method used to parsing 500 error and try to convert it to 404 error according to the right error code. Arguments: + err: The error response obtained through HTTP/HTTPS request. + errCodeKey: The key name of the error code in the error response body, e.g. 'error_code', 'err_code'. + specErrCodes: One or more error codes that you wish to match against the current error, e.g. 'APIGW.0001'. Notes: If you missing specErrCodes input, this function will convert all 500 errors into 404 errors. How to use it: + For the general cases, their error code key is 'error_code', and we should call as follow:

  • utils.ConvertExpected500ErrInto404Err(err, "error_code")
  • utils.ConvertExpected500ErrInto404Err(err, "error_code", "SCM.0016")
  • utils.ConvertExpected500ErrInto404Err(err, "error_code", []string{"SCM.0016", "SCM.0017"}...)

func ConvertUndefinedErrInto404Err added in v1.70.0

func ConvertUndefinedErrInto404Err(err error, errStatusNum int, errCodeKey string, specErrCodes ...string) error

ConvertUndefinedErrInto404Err is a method used to parsing errors which related structures undefined in the golangsdk package (so a general method for parsing such errors is needed) and try to convert it to 404 error according to the right error number and code (omitted means all passed). Unsupported status codes (may not enter the convert logic and returned directly):

  • 400, 401, 402, 403, 404, 405, 408, 429, 500, 503

Arguments:

  • err: The error response obtained through HTTP/HTTPS request.
  • errStatusNum: The status number of the error, e.g. 409, 415 (The common status numbers are defined in the golangsdk package, e.g. 400, 403, 500)
  • errCodeKey: The key name of the error code in the error response body, e.g. 'error_code', 'err_code'.
  • specErrCodes: One or more error codes that you wish to match against the current error, e.g. 'CSS.0004'.

Notes: If you missing specErrCodes input, this function will convert all errors which matched your specifies status into 404 errors.

How to use it:

  • utils.ConvertUndefinedErrInto404Err(err, 415, "error_code")
  • utils.ConvertUndefinedErrInto404Err(err, 415, "error_code", "CSS.0004")
  • utils.ConvertUndefinedErrInto404Err(err, 415, "error_code", []string{"CSS.0004", "CSS.0005"}...)
  • utils.ConvertUndefinedErrInto404Err(err, 415, "")

The corresponding processing of this method are as follows:

  • The status code has a corresponding structure definition in golangsdk: the error is recorded in the log and the original error is returned directly.
  • The status code (key) not found: return error (404 error: if the errCodeKey is omitted).
  • The input of the expected error code(s) is omitted: return 404 error.
  • The expected error code(s) matched: return 404 error.
  • Other situations: return original error.

func GetAutoPay added in v1.37.0

func GetAutoPay(d *schema.ResourceData) string

GetAutoPay is a method to return whether order is auto pay according to the user input. auto_pay parameter inputs and returns:

false: false
true, empty: true

Before using this function, make sure the parameter behavior is auto pay (the default value is "true").

func GetEipIDbyAddress added in v1.30.0

func GetEipIDbyAddress(client *golangsdk.ServiceClient, address, epsID string) (string, error)

GetEipIDbyAddress returns the EIP ID of address when success.

func GetEipsbyAddresses added in v1.61.1

func GetEipsbyAddresses(client *golangsdk.ServiceClient, addresses []string, epsID string) ([]eips.PublicIp, error)

GetEipsbyAddresses returns the EIPs of addresses when success.

func GetRegion

func GetRegion(d *schema.ResourceData, config *config.Config) string

GetRegion returns the region that was specified ina the resource. If a region was not set, the provider-level region is checked. The provider-level region can either be set by the region argument or by HW_REGION_NAME.

func GetResourceIDsByOrder added in v1.63.0

func GetResourceIDsByOrder(client *golangsdk.ServiceClient, orderId string, onlyMainResource int) ([]string, error)

GetResourceIDsByOrder returns resource IDs from an order.

func HasFilledOpt added in v1.41.0

func HasFilledOpt(d *schema.ResourceData, param string) bool

func NewCustomClient added in v1.38.0

func NewCustomClient(insecure bool, endpoints ...string) *golangsdk.ServiceClient

NewCustomClient creates a custom client assembled from user-provided endpoints. URLs will be assembled according to the endpoints array, separated each element by slashes. for example, array ["https://www.example.com", "v2", "test", ...] will form the address "https://www.example.com/v2/test/.../". NOTE: You can decide whether to skip the SSL certificate check with the insecure parameter.

func RetryContextWithWaitForState added in v1.52.0

func RetryContextWithWaitForState(param *RetryContextWithWaitForStateParam) (interface{}, error)

RetryContextWithWaitForState The RetryFunc will be called first if the error of the return is nil, the retry will be ended and the res of the return will be returned if the retry of the return is true, the RetryFunc will be retried, and the WaitFunc will be called if it is not nil if the retry of the return is false, the retry will be ended and the error of the retry func will be returned

func SchemaAutoPay added in v1.35.0

func SchemaAutoPay(conflicts []string) *schema.Schema

func SchemaAutoRenew added in v1.28.1

func SchemaAutoRenew(conflicts []string) *schema.Schema

func SchemaAutoRenewUpdatable added in v1.40.2

func SchemaAutoRenewUpdatable(conflicts []string) *schema.Schema

func SchemaChargingMode added in v1.35.0

func SchemaChargingMode(conflicts []string) *schema.Schema

func SchemaPeriod added in v1.28.1

func SchemaPeriod(conflicts []string) *schema.Schema

func SchemaPeriodUnit added in v1.28.1

func SchemaPeriodUnit(conflicts []string) *schema.Schema

func TagsComputedSchema added in v1.47.0

func TagsComputedSchema(description ...string) *schema.Schema

TagsComputedSchema returns the schema to use for tags as an attribute.

func TagsForceNewSchema added in v1.28.1

func TagsForceNewSchema(description ...string) *schema.Schema

TagsForceNewSchema returns the schema to use for tags with ForceNew behavior.

func TagsSchema added in v1.28.0

func TagsSchema(description ...string) *schema.Schema

TagsSchema returns the schema to use for tags.

func UnsubscribePrePaidResource

func UnsubscribePrePaidResource(d *schema.ResourceData, config *config.Config, resourceIDs []string) error

UnsubscribePrePaidResource impl the action of unsubscribe resource

func UpdateAutoRenew added in v1.40.1

func UpdateAutoRenew(c *golangsdk.ServiceClient, enabled, resourceId string) error

func UpdateEcsInstanceKeyPair added in v1.44.1

func UpdateEcsInstanceKeyPair(ctx context.Context, ecsClient, kmsClient *golangsdk.ServiceClient, opts *KeypairAuthOpts) error

func ValidatePrePaidChargeInfo added in v1.28.1

func ValidatePrePaidChargeInfo(d *schema.ResourceData) error

func WaitOrderAllResourceComplete added in v1.62.0

func WaitOrderAllResourceComplete(ctx context.Context, client *golangsdk.ServiceClient, orderId string,
	timeout time.Duration) (string, error)

WaitOrderAllResourceComplete is the method to wait for the non-main resource to be generated. Notes: Note that this method needs to be used in conjunction with method "WaitOrderComplete", because the ID of some resources may not be generated when the order is not completed.

func WaitOrderComplete added in v1.29.0

func WaitOrderComplete(ctx context.Context, client *golangsdk.ServiceClient, orderId string,
	timeout time.Duration) error

func WaitOrderResourceComplete added in v1.41.0

func WaitOrderResourceComplete(ctx context.Context, client *golangsdk.ServiceClient, orderId string,
	timeout time.Duration) (string, error)

WaitOrderResourceComplete is the method to wait for the resource to be generated. Notes: Note that this method needs to be used in conjunction with method "WaitOrderComplete", because the ID of some resources may not be generated when the order is not completed.

Types

type ErrorResp added in v1.41.0

type ErrorResp struct {
	ErrorCode string `json:"error_code"`
	ErrorMsg  string `json:"error_msg"`
}

ErrorResp is the response when API failed

func ParseErrorMsg added in v1.41.0

func ParseErrorMsg(body []byte) (ErrorResp, error)

type KeypairAuthOpts added in v1.44.1

type KeypairAuthOpts struct {
	// the ECS instance ID
	InstanceID string
	// the keypair name in used
	InUsedKeyPair string
	// the replaced keypair name
	NewKeyPair string
	// the private key of the keypair name in used, it's used to replace or unbind the keypair
	InUsedPrivateKey string
	// the root password of the ECS instance, it's used to bind a new keypair
	Password string
	// whether to disable SSH login on the VM
	DisablePassword bool
	// the timeout to wait for the task
	Timeout time.Duration
}

type RetryContextWithWaitForStateParam added in v1.52.0

type RetryContextWithWaitForStateParam struct {
	Ctx context.Context
	// The func that need to be retried
	RetryFunc RetryFunc
	// The wait func when the retry which returned by the retry func is true
	WaitFunc resource.StateRefreshFunc
	// The target of the wait func
	WaitTarget []string
	// The pending of the wait func
	WaitPending []string
	// The timeout of the retry func and wait func
	Timeout time.Duration
	// The delay timeout of the retry func and wait func
	DelayTimeout time.Duration
	// The poll interval of the retry func and wait func
	PollInterval time.Duration
}

type RetryFunc added in v1.52.0

type RetryFunc func() (res interface{}, retry bool, err error)

RetryFunc is the function retried until it succeeds. The first return parameter is the result of the retry func. The second return parameter indicates whether a retry is required. The last return parameter is the error of the func.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL