utils

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package utils contains helper functions that can be used by all products.

Index

Examples

Constants

View Source
const DefaultErrMsg = "An error has occurred in the program. Please consider opening an issue."

Variables

This section is empty.

Functions

func AdaptBoolPointerValueToNullableBool

func AdaptBoolPointerValueToNullableBool(value types.Bool) *bool

AdaptBoolPointerValueToNullableBool converts a Terraform BoolPointerValue to a nullable string.

Example
value := true
terraformBoolPointerValue := basetypes.NewBoolPointerValue(&value)

convertedValue := AdaptBoolPointerValueToNullableBool(terraformBoolPointerValue)

fmt.Println(*convertedValue)
Output:

true

func AdaptInt32PointerValueToNullableInt32

func AdaptInt32PointerValueToNullableInt32(int32Type types.Int32) *int32

AdaptInt32PointerValueToNullableInt32 converts a Terraform Int32PointerValue to a nullable int32.

Example
value := int32(3)
int32Value := basetypes.NewInt32PointerValue(&value)
adaptedValue := AdaptInt32PointerValueToNullableInt32(int32Value)

fmt.Println(*adaptedValue)
Output:

3
Example (Second)
int32Value := basetypes.NewInt32PointerValue(nil)
adaptedValue := AdaptInt32PointerValueToNullableInt32(int32Value)

fmt.Println(adaptedValue)
Output:

<nil>

func AdaptNullableSdkModelToResourceObject added in v1.12.0

func AdaptNullableSdkModelToResourceObject[T interface{}, U interface{}](
	sdkModel *T,
	attributeTypes map[string]attr.Type,
	ctx context.Context,
	generateResourceObject func(sdkModel T) U,
) (basetypes.ObjectValue, error)

AdaptNullableSdkModelToResourceObject converts a nullable sdk model to a Terraform resource object.

Example
type Image struct {
	Id types.String `tfsdk:"id"`
}

resourceModel, _ := AdaptNullableSdkModelToResourceObject(
	&publiccloud.Image{Id: "imageId"},
	map[string]attr.Type{
		"id": types.StringType,
	},
	context.TODO(),
	func(image publiccloud.Image) Image {
		return Image{
			Id: basetypes.NewStringValue(image.Id),
		}
	},
)

fmt.Println(resourceModel)
Output:

{"id":"imageId"}
Example (Second)
type Image struct {
	Id types.String `tfsdk:"id"`
}

resourceModel, _ := AdaptNullableSdkModelToResourceObject(
	nil,
	map[string]attr.Type{
		"id": types.StringType,
	},
	context.TODO(),
	func(image publiccloud.Image) Image {
		return Image{
			Id: basetypes.NewStringValue(image.Id),
		}
	},
)

fmt.Println(resourceModel)
Output:

<null>

func AdaptNullableTimeToStringValue

func AdaptNullableTimeToStringValue(value *time.Time) basetypes.StringValue

AdaptNullableTimeToStringValue converts a nullable Time to a Terraform StringValue.

Example
nullableTime, _ := time.Parse(time.RFC3339, "2019-09-08T00:00:00Z")
value := AdaptNullableTimeToStringValue(&nullableTime)

fmt.Println(value)
Output:

"2019-09-08 00:00:00 +0000 UTC"
Example (Second)
value := AdaptNullableTimeToStringValue(nil)

fmt.Println(value)
Output:

<null>

func AdaptSdkModelToResourceObject

func AdaptSdkModelToResourceObject[T any, U any](
	sdkModel T,
	attributeTypes map[string]attr.Type,
	ctx context.Context,
	generateResourceObject func(sdkModel T) U,
) (basetypes.ObjectValue, error)

AdaptSdkModelToResourceObject converts an sdk model to a Terraform resource object.

Example
type Image struct {
	Id types.String `tfsdk:"id"`
}

resourceModel, _ := AdaptSdkModelToResourceObject(
	publiccloud.Image{Id: "imageId"},
	map[string]attr.Type{
		"id": types.StringType,
	},
	context.TODO(),
	func(image publiccloud.Image) Image {
		return Image{
			Id: basetypes.NewStringValue(image.Id),
		}
	},
)

fmt.Println(resourceModel)
Output:

{"id":"imageId"}

func AdaptSdkModelsToListValue

func AdaptSdkModelsToListValue[T any, U any](
	sdkModels []T,
	attributeTypes map[string]attr.Type,
	ctx context.Context,
	generateModel func(sdkModel T) U,
) (basetypes.ListValue, error)

AdaptSdkModelsToListValue converts a sdk model array to a Terraform ListValue.

Example
type Ip struct {
	Ip types.String `tfsdk:"ip"`
}

listValue, _ := AdaptSdkModelsToListValue(
	[]publiccloud.Ip{{Ip: "1.2.3.4"}},
	map[string]attr.Type{
		"ip": types.StringType,
	},
	context.TODO(),
	func(ip publiccloud.Ip) Ip {
		return Ip{
			Ip: basetypes.NewStringValue(ip.Ip),
		}
	},
)

fmt.Println(listValue)
Output:

[{"ip":"1.2.3.4"}]

func AdaptStringPointerValueToNullableString

func AdaptStringPointerValueToNullableString(value types.String) *string

AdaptStringPointerValueToNullableString converts a Terraform StringPointerValue to a nullable string.

Example
value := "tralala"
terraformStringPointerValue := basetypes.NewStringPointerValue(&value)

convertedValue := AdaptStringPointerValueToNullableString(terraformStringPointerValue)

fmt.Println(*convertedValue)
Output:

tralala
Example (Second)
terraformStringPointerValue := basetypes.NewStringPointerValue(nil)

convertedValue := AdaptStringPointerValueToNullableString(terraformStringPointerValue)

fmt.Println(convertedValue)
Output:

<nil>

func AdaptStringTypeArrayToStringArray

func AdaptStringTypeArrayToStringArray[T ~string](types []T) []string
Example
type customType string
customTypes := []customType{customType("value")}

stringTypes := AdaptStringTypeArrayToStringArray(customTypes)

fmt.Println(stringTypes)
Output:

[value]

func AddUnsupportedActionsNotation added in v1.11.0

func AddUnsupportedActionsNotation(
	response *resource.SchemaResponse,
	unsupportedActions []Action,
)

AddUnsupportedActionsNotation lets the end user know which actions aren't supported in the markdown description.

Example
response := resource.SchemaResponse{}
AddUnsupportedActionsNotation(&response, []Action{UpdateAction})

fmt.Println(response.Schema.GetMarkdownDescription())
Output:

**Note:**
- Once created, this resource cannot be updated.

func Error

func Error(
	ctx context.Context,
	diags *diag.Diagnostics,
	summary string,
	err error,
	resp *http.Response,
)
Example
diags := diag.Diagnostics{}

httpResponse := http.Response{
	StatusCode: 500,
	Body: io.NopCloser(
		bytes.NewReader(
			[]byte(`
{
  "correlationId": "correlationId",
  "errorCode": "errorCode",
  "errorMessage": "errorMessage",
  "errorDetails":  {
  "name": ["the name is invalid"]
  }
}
          `),
		),
	),
}

Error(
	context.TODO(),
	&diags,
	"summary",
	errors.New("error content"),
	&httpResponse,
)

fmt.Println(diags.Errors())
Output:

[{{the name is invalid summary} {[name]}}]

func NewOffset

func NewOffset(limit, offset, totalCount int32) *int32
Example
offset := NewOffset(0, 5, 12)
fmt.Println(*offset)
Output:

5
Example (Second)
offset := NewOffset(10, 12, 5)
fmt.Println(offset)
Output:

<nil>

func ReturnError

func ReturnError(functionName string, diags diag.Diagnostics) error

ReturnError returns the first diagnostics error as a golang Error.

Example
diags := diag.Diagnostics{}
diags.AddError("summary", "detail")

returnedErrors := ReturnError("functionName", diags)

fmt.Println(returnedErrors)
Output:

functionName: "summary" "detail"

func StringTypeArrayToMarkdown

func StringTypeArrayToMarkdown[T ~string](enumValues []T) string

StringTypeArrayToMarkdown converts any slice of string enums that are underlying string types to a Markdown list string.

Example
type underlyingString string

enumValues := []underlyingString{
	"TEST_ONE",
	"TEST_TWO",
}

markdown := StringTypeArrayToMarkdown(enumValues)

fmt.Println(markdown)
// Output "\n  - *TEST_ONE*\n  - *TEST_TWO*\n"
Output:

Types

type Action added in v1.11.0

type Action int
const (
	CreateAction Action = iota
	ReadAction
	UpdateAction
	DeleteAction
)

type IntMarkdownList

type IntMarkdownList []int

IntMarkdownList implements helpers to use int32 sets in validation & documentation.

func NewIntMarkdownList

func NewIntMarkdownList[T ~int32](values []T) IntMarkdownList

NewIntMarkdownList instantiates a new IntMarkdownList.

func (IntMarkdownList) Markdown

func (i IntMarkdownList) Markdown() string

Markdown returns a string with all the values in Markdown list format.

Example
list := IntMarkdownList{1, 2, 3}

fmt.Println(list.Markdown())
/**
  Output:
  - *1*
  - *2*
  - *3*
*/
Output:

func (IntMarkdownList) ToInt32

func (i IntMarkdownList) ToInt32() []int32

ToInt32 converts all slice values to int32.

Example
list := IntMarkdownList{1, 2, 3}.ToInt32()

fmt.Println(list)
// Output []{1, 2, 3}
Output:

Jump to

Keyboard shortcuts

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