github.com/crossplane/upjet@v1.3.0/pkg/resource/fake/terraformed.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package fake
     6  
     7  import (
     8  	"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
     9  	"k8s.io/apimachinery/pkg/runtime"
    10  	"k8s.io/apimachinery/pkg/runtime/schema"
    11  	"k8s.io/apimachinery/pkg/util/json"
    12  )
    13  
    14  // Observable is mock Observable.
    15  type Observable struct {
    16  	Observation                 map[string]any
    17  	AdditionalConnectionDetails map[string][]byte
    18  	ID                          string
    19  }
    20  
    21  // GetObservation is a mock.
    22  func (o *Observable) GetObservation() (map[string]any, error) {
    23  	return o.Observation, nil
    24  }
    25  
    26  // SetObservation is a mock.
    27  func (o *Observable) SetObservation(data map[string]any) error {
    28  	o.Observation = data
    29  	return nil
    30  }
    31  
    32  // GetID is a mock.
    33  func (o *Observable) GetID() string {
    34  	return o.ID
    35  }
    36  
    37  // GetAdditionalConnectionDetails is a mock
    38  func (o *Observable) GetAdditionalConnectionDetails(_ map[string]any) (map[string][]byte, error) {
    39  	return o.AdditionalConnectionDetails, nil
    40  }
    41  
    42  // Parameterizable is mock Parameterizable.
    43  type Parameterizable struct {
    44  	Parameters     map[string]any
    45  	InitParameters map[string]any
    46  }
    47  
    48  func (t *Terraformed) GetMergedParameters(_ bool) (map[string]any, error) {
    49  	return t.Parameters, nil
    50  }
    51  
    52  // GetParameters is a mock.
    53  func (p *Parameterizable) GetParameters() (map[string]any, error) {
    54  	return p.Parameters, nil
    55  }
    56  
    57  // SetParameters is a mock.
    58  func (p *Parameterizable) SetParameters(data map[string]any) error {
    59  	p.Parameters = data
    60  	return nil
    61  }
    62  
    63  // GetInitParameters is a mock.
    64  func (p *Parameterizable) GetInitParameters() (map[string]any, error) {
    65  	return p.InitParameters, nil
    66  }
    67  
    68  // MetadataProvider is mock MetadataProvider.
    69  type MetadataProvider struct {
    70  	Type                     string
    71  	SchemaVersion            int
    72  	ConnectionDetailsMapping map[string]string
    73  }
    74  
    75  // GetTerraformResourceType is a mock.
    76  func (mp *MetadataProvider) GetTerraformResourceType() string {
    77  	return mp.Type
    78  }
    79  
    80  // GetTerraformSchemaVersion is a mock.
    81  func (mp *MetadataProvider) GetTerraformSchemaVersion() int {
    82  	return mp.SchemaVersion
    83  }
    84  
    85  // GetConnectionDetailsMapping is a mock.
    86  func (mp *MetadataProvider) GetConnectionDetailsMapping() map[string]string {
    87  	return mp.ConnectionDetailsMapping
    88  }
    89  
    90  // LateInitializer is mock LateInitializer.
    91  type LateInitializer struct {
    92  	Result bool
    93  	Err    error
    94  }
    95  
    96  // LateInitialize is a mock.
    97  func (li *LateInitializer) LateInitialize(_ []byte) (bool, error) {
    98  	return li.Result, li.Err
    99  }
   100  
   101  // Terraformed is a mock that implements Terraformed interface.
   102  type Terraformed struct {
   103  	fake.Managed
   104  	Observable
   105  	Parameterizable
   106  	MetadataProvider
   107  	LateInitializer
   108  }
   109  
   110  // GetObjectKind returns schema.ObjectKind.
   111  func (t *Terraformed) GetObjectKind() schema.ObjectKind {
   112  	return schema.EmptyObjectKind
   113  }
   114  
   115  // DeepCopyObject returns a copy of the object as runtime.Object
   116  func (t *Terraformed) DeepCopyObject() runtime.Object {
   117  	out := &Terraformed{}
   118  	j, err := json.Marshal(t)
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  	_ = json.Unmarshal(j, out)
   123  	return out
   124  }
   125  
   126  // Option is an option to modify the properties of a Terraformed object.
   127  type Option func(terraformed *Terraformed)
   128  
   129  // WithParameters sets the parameters of a Terraformed.
   130  func WithParameters(params map[string]any) Option {
   131  	return func(tr *Terraformed) {
   132  		tr.Parameters = params
   133  	}
   134  }
   135  
   136  // NewTerraformed initializes a new Terraformed with the given options.
   137  func NewTerraformed(opts ...Option) *Terraformed {
   138  	tr := &Terraformed{}
   139  	for _, o := range opts {
   140  		o(tr)
   141  	}
   142  	return tr
   143  }
   144  
   145  // NewMap prepares a map from the supplied key value parameters.
   146  // The parameters slice must be a sequence of key, value pairs and must have
   147  // an even length. The function will panic otherwise.
   148  func NewMap(keyValue ...string) map[string]any {
   149  	m := make(map[string]any, len(keyValue)/2)
   150  	for i := 0; i < len(keyValue)-1; i += 2 {
   151  		m[keyValue[i]] = keyValue[i+1]
   152  	}
   153  	return m
   154  }