github.com/crossplane/upjet@v1.3.0/pkg/resource/json/statev4.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package json
     6  
     7  import (
     8  	jsoniter "github.com/json-iterator/go"
     9  )
    10  
    11  // NewStateV4 returns a new base StateV4 object.
    12  func NewStateV4() *StateV4 {
    13  	return &StateV4{
    14  		Version: 4,
    15  		Serial:  1,
    16  	}
    17  }
    18  
    19  // State file schema from https://github.com/hashicorp/terraform/blob/d9dfd451ea572219871bb9c5503a471418258e40/internal/states/statefile/version4.go
    20  
    21  // StateV4 represents a version 4 terraform state
    22  type StateV4 struct {
    23  	Version          uint64                   `json:"version"`
    24  	TerraformVersion string                   `json:"terraform_version"`
    25  	Serial           uint64                   `json:"serial"`
    26  	Lineage          string                   `json:"lineage"`
    27  	RootOutputs      map[string]OutputStateV4 `json:"outputs"`
    28  	Resources        []ResourceStateV4        `json:"resources"`
    29  }
    30  
    31  // OutputStateV4 represents a version 4 output state
    32  type OutputStateV4 struct {
    33  	ValueRaw     jsoniter.RawMessage `json:"value"`
    34  	ValueTypeRaw jsoniter.RawMessage `json:"type"`
    35  	Sensitive    bool                `json:"sensitive,omitempty"`
    36  }
    37  
    38  // ResourceStateV4 represents a version 4 resource state
    39  type ResourceStateV4 struct {
    40  	Module         string                  `json:"module,omitempty"`
    41  	Mode           string                  `json:"mode"`
    42  	Type           string                  `json:"type"`
    43  	Name           string                  `json:"name"`
    44  	EachMode       string                  `json:"each,omitempty"`
    45  	ProviderConfig string                  `json:"provider"`
    46  	Instances      []InstanceObjectStateV4 `json:"instances"`
    47  }
    48  
    49  // InstanceObjectStateV4 represents a version 4 instance object state
    50  type InstanceObjectStateV4 struct {
    51  	IndexKey any    `json:"index_key,omitempty"`
    52  	Status   string `json:"status,omitempty"`
    53  	Deposed  string `json:"deposed,omitempty"`
    54  
    55  	SchemaVersion           uint64              `json:"schema_version"`
    56  	AttributesRaw           jsoniter.RawMessage `json:"attributes,omitempty"`
    57  	AttributesFlat          map[string]string   `json:"attributes_flat,omitempty"`
    58  	AttributeSensitivePaths jsoniter.RawMessage `json:"sensitive_attributes,omitempty"`
    59  
    60  	PrivateRaw []byte `json:"private,omitempty"`
    61  
    62  	Dependencies []string `json:"dependencies,omitempty"`
    63  
    64  	CreateBeforeDestroy bool `json:"create_before_destroy,omitempty"`
    65  }
    66  
    67  // GetAttributes returns attributes of the Terraform managed resource (i.e. first instance of first resource)
    68  func (st *StateV4) GetAttributes() jsoniter.RawMessage {
    69  	if st == nil || len(st.Resources) == 0 || len(st.Resources[0].Instances) == 0 {
    70  		return nil
    71  	}
    72  	return st.Resources[0].Instances[0].AttributesRaw
    73  }
    74  
    75  // GetSensitiveAttributes returns sensitive attributes of the Terraform managed resource (i.e. first instance of first resource)
    76  func (st *StateV4) GetSensitiveAttributes() jsoniter.RawMessage {
    77  	if st == nil || len(st.Resources) == 0 || len(st.Resources[0].Instances) == 0 {
    78  		return nil
    79  	}
    80  	return st.Resources[0].Instances[0].AttributeSensitivePaths
    81  }
    82  
    83  // GetPrivateRaw returns private attribute of the Terraform managed resource
    84  // that is used as metadata by the Terraform provider
    85  func (st *StateV4) GetPrivateRaw() []byte {
    86  	if st == nil || len(st.Resources) == 0 || len(st.Resources[0].Instances) == 0 {
    87  		return nil
    88  	}
    89  	return st.Resources[0].Instances[0].PrivateRaw
    90  }