Skip to content

Commit

Permalink
feature(main): add tf as infra
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <[email protected]>
  • Loading branch information
cuisongliu committed Oct 4, 2023
1 parent 1726cca commit 5896ea0
Show file tree
Hide file tree
Showing 14 changed files with 1,445 additions and 30 deletions.
19 changes: 10 additions & 9 deletions test/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ replace (
require (
github.com/BurntSushi/toml v1.3.2
github.com/Masterminds/sprig/v3 v3.2.3
github.com/go-bindata/go-bindata v1.0.0
github.com/google/go-containerregistry v0.15.2
github.com/google/uuid v1.3.0
github.com/labring/image-cri-shim v0.0.0
github.com/labring/sealos v0.0.0
Expand All @@ -28,8 +30,9 @@ require (
github.com/onsi/gomega v1.27.8
github.com/pkg/errors v0.9.1
golang.org/x/sync v0.2.0
k8s.io/api v0.27.1
k8s.io/apimachinery v0.27.1
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
k8s.io/cri-api v0.27.1
k8s.io/kube-proxy v0.25.6
k8s.io/kubelet v0.25.6
Expand All @@ -40,7 +43,7 @@ require (
require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.10.0-rc.8 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
Expand Down Expand Up @@ -107,7 +110,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-containerregistry v0.15.2 // indirect
github.com/google/go-intervals v0.0.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230323073829-e72429f035bd // indirect
Expand All @@ -125,7 +127,7 @@ require (
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/labring/sealos/controllers/cluster v0.0.0-20230417222036-c48922461ea6 // indirect
github.com/labring/sealos/controllers/common v0.0.0-20230528031239-655196af17c7 // indirect
github.com/labring/sreg v0.1.1 // indirect
github.com/letsencrypt/boulder v0.0.0-20230213213521-fdfea0d469b6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
Expand Down Expand Up @@ -187,7 +189,7 @@ require (
github.com/vishvananda/netlink v1.2.1-beta.2 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.mongodb.org/mongo-driver v1.11.3 // indirect
go.mongodb.org/mongo-driver v1.11.4 // indirect
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
Expand All @@ -214,11 +216,10 @@ require (
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/client-go v0.26.2 // indirect
k8s.io/cluster-bootstrap v0.25.6 // indirect
k8s.io/component-base v0.26.2 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/controller-runtime v0.13.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
29 changes: 17 additions & 12 deletions test/e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
Expand Down Expand Up @@ -155,6 +156,8 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
github.com/fsouza/go-dockerclient v1.9.7 h1:FlIrT71E62zwKgRvCvWGdxRD+a/pIy+miY/n3MXgfuw=
github.com/fsouza/go-dockerclient v1.9.7/go.mod h1:vx9C32kE2D15yDSOMCDaAEIARZpDQDFBHeqL3MgQy/U=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-bindata/go-bindata v1.0.0 h1:DZ34txDXWn1DyWa+vQf7V9ANc2ILTtrEjtlsdJRF26M=
github.com/go-bindata/go-bindata v1.0.0/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -336,6 +339,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labring/sreg v0.1.1 h1:JVI2FwdSQMPgQGRrTdsM6SEfy7eKd0F93VkweoNp3b4=
github.com/labring/sreg v0.1.1/go.mod h1:d519C7n2ekJJX6KxsUV78MeKO5Zon+atP4FkfwB++DM=
github.com/letsencrypt/boulder v0.0.0-20230213213521-fdfea0d469b6 h1:unJdfS94Y3k85TKy+mvKzjW5R9rIC+Lv4KGbE7uNu0I=
github.com/letsencrypt/boulder v0.0.0-20230213213521-fdfea0d469b6/go.mod h1:PUgW5vI9ANEaV6qv9a6EKu8gAySgwf0xrzG9xIB/CK0=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -459,7 +464,7 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.8.6 h1:QruMUdzZ1TbEP++S1m73OqRJk20ON11m6Wqv4EoGg8c=
github.com/schollz/progressbar/v3 v3.8.6/go.mod h1:W5IEwbJecncFGBvuEh4A7HT1nZZ6WNIL2i3qbnI0WKY=
Expand Down Expand Up @@ -559,8 +564,8 @@ go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.mongodb.org/mongo-driver v1.7.3/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg=
go.mongodb.org/mongo-driver v1.7.5/go.mod h1:VXEWRZ6URJIkUq2SCAyapmhH0ZLRBP+FT4xhp5Zvxng=
go.mongodb.org/mongo-driver v1.10.0/go.mod h1:wsihk0Kdgv8Kqu1Anit4sfK+22vSFbUrAVEYRhCXrA8=
go.mongodb.org/mongo-driver v1.11.3 h1:Ql6K6qYHEzB6xvu4+AU0BoRoqf9vFPcc4o7MUIdPW8Y=
go.mongodb.org/mongo-driver v1.11.3/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.mongodb.org/mongo-driver v1.11.4 h1:4ayjakA013OdpGyL2K3ZqylTac/rMjrJOMZ1EHizXas=
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1/go.mod h1:SNgMg+EgDFwmvSmLRTNKC5fegJjB7v23qTQ0XLGUNHk=
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 h1:CCriYyAfq1Br1aIYettdHZTy8mBTIPo7We18TuO/bak=
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352/go.mod h1:SNgMg+EgDFwmvSmLRTNKC5fegJjB7v23qTQ0XLGUNHk=
Expand Down Expand Up @@ -792,22 +797,22 @@ gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.27.1 h1:Z6zUGQ1Vd10tJ+gHcNNNgkV5emCyW+v2XTmn+CLjSd0=
k8s.io/api v0.27.1/go.mod h1:z5g/BpAiD+f6AArpqNjkY+cji8ueZDU/WV1jcj5Jk4E=
k8s.io/apimachinery v0.27.1 h1:EGuZiLI95UQQcClhanryclaQE6xjg1Bts6/L3cD7zyc=
k8s.io/apimachinery v0.27.1/go.mod h1:5ikh59fK3AJ287GUvpUsryoMFtH9zj/ARfWCo3AyXTM=
k8s.io/api v0.27.4 h1:0pCo/AN9hONazBKlNUdhQymmnfLRbSZjd5H5H3f0bSs=
k8s.io/api v0.27.4/go.mod h1:O3smaaX15NfxjzILfiln1D8Z3+gEYpjEpiNA/1EVK1Y=
k8s.io/apimachinery v0.27.4 h1:CdxflD4AF61yewuid0fLl6bM4a3q04jWel0IlP+aYjs=
k8s.io/apimachinery v0.27.4/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E=
k8s.io/client-go v0.25.6 h1:CHxACHi0DijmlYyUR7ooZoXnD5P8jYLgBHcxp775x/U=
k8s.io/client-go v0.25.6/go.mod h1:s9mMAGFYiH3Z66j7BESzu0GEradT9GQ2LjFf/YRrnyc=
k8s.io/cluster-bootstrap v0.25.6 h1:OuRfdWUxuf/ZzzAAHDfOR+Eb6ipp6ioxbDhmMilKSpg=
k8s.io/cluster-bootstrap v0.25.6/go.mod h1:oykEiqdSNCkiZsiyl8z1wnFbqVvSWbI6fYUPKoXjv7U=
k8s.io/component-base v0.26.2 h1:IfWgCGUDzrD6wLLgXEstJKYZKAFS2kO+rBRi0p3LqcI=
k8s.io/component-base v0.26.2/go.mod h1:DxbuIe9M3IZPRxPIzhch2m1eT7uFrSBJUBuVCQEBivs=
k8s.io/component-base v0.27.2 h1:neju+7s/r5O4x4/txeUONNTS9r1HsPbyoPBAtHsDCpo=
k8s.io/component-base v0.27.2/go.mod h1:5UPk7EjfgrfgRIuDBFtsEFAe4DAvP3U+M8RTzoSJkpo=
k8s.io/cri-api v0.25.6 h1:ueaPYiflK9pvSP+GXgCmklG8Xp63foNYnZpnuiJghtA=
k8s.io/cri-api v0.25.6/go.mod h1:fg+6ctfBFAUYnKfjmYqUVXwq6A788L0ZvNooI405Nek=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a h1:gmovKNur38vgoWfGtP5QOGNOA7ki4n6qNYoFAgMlNvg=
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a/go.mod h1:y5VtZWM9sHHc2ZodIH/6SHzXj+TPU5USoA8lcIeKEKY=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f/go.mod h1:byini6yhqGC14c3ebc/QwanvYwhuMWF6yz2F8uwW8eg=
k8s.io/kube-proxy v0.25.6 h1:vG8Zg8xMFOGg2gnEkmYuSU/RJ4waIOW6Ti6/Y8at26M=
k8s.io/kube-proxy v0.25.6/go.mod h1:uMzY4OdkcMQ+sNAnze9iyKdff035UfCDc1k6dLOjj8Q=
k8s.io/kubelet v0.25.6 h1:rZntZUVRlZ1nys1C9QBPJ2drzy6Mq67yfgmDlFzrQHw=
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ variable "region" {
}

variable "image_id" {
# default = "ubuntu_22_04_arm64_20G_alibase_20230712.vhd"
default = "ubuntu_22_04_x64_20G_alibase_20230613.vhd"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ variable "region" {
}

variable "image_id" {
# default = "ubuntu_22_04_arm64_20G_alibase_20230712.vhd"
default = "ubuntu_22_04_x64_20G_alibase_20230613.vhd"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ variable "region" {

variable "image_id" {
default = "ubuntu_22_04_arm64_20G_alibase_20230712.vhd"
# default = "ubuntu_22_04_x64_20G_alibase_20230613.vhd"
}

variable "ecs_type" {
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/terraform/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 [email protected].
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
bindata "github.com/go-bindata/go-bindata"

"github.com/labring/sealos/pkg/utils/logger"
)

func main() {
logger.Info("generator bindata start")
bc := &bindata.Config{
Input: []bindata.InputConfig{
{
Path: "infra",
Recursive: false,
},
},
Package: "main",
NoCompress: true,
NoMemCopy: true,
NoMetadata: true,
Output: "zz_generated_terraform.go",
}
if err := bindata.Translate(bc); err != nil {
logger.Fatal(err)
}
logger.Info("generator bindata success")
}
168 changes: 168 additions & 0 deletions test/e2e/terraform/terraform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
Copyright 2023 [email protected].
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"fmt"
"os"
"path"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"

"k8s.io/client-go/util/homedir"

executils "github.com/labring/sealos/pkg/utils/exec"
"github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/logger"
)

//go:generate go run main.go

var HomeDir string

type Terraform struct {
accessKey string
secretKey string
}

func (tf *Terraform) Apply(architecture string) error {
_, ok := executils.CheckCmdIsExist("terraform")
if !ok {
return fmt.Errorf("not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ")
}
tf.setEnv()
defer func() {
tf.unsetEnv()
}()
file.CleanDir(HomeDir)
_ = file.MkDirs(HomeDir)
files := AssetNames()
for _, f := range files {
if f == "infra/vars.tf" {
logger.Warn("temp var tf file, skip generator")
continue
}
data, err := Asset(f)
if err != nil {
return err
}
if f == fmt.Sprintf("infra/vars.tf.%s", architecture) {
if err = file.WriteFile(path.Join(HomeDir, "vars.tf"), data); err != nil {
return err
}
}
parts := strings.SplitN(f, "/", 2)
if len(parts) == 2 {
if err = file.WriteFile(path.Join(HomeDir, parts[1]), data); err != nil {
return err
}
}
}
_ = file.CleanFiles(path.Join(HomeDir, "vars.tf.amd64"), path.Join(HomeDir, "vars.tf.arm64"))
err := executils.CmdWithContext(context.Background(), "bash", "-c", fmt.Sprintf("cd %s && terraform init", HomeDir))
if err != nil {
return err
}
err = executils.CmdWithContext(context.Background(), "bash", "-c", fmt.Sprintf("cd %s && terraform apply -auto-approve", HomeDir))
if err != nil {
return err
}
return err
}

func (tf *Terraform) Destroy() error {
_, ok := executils.CheckCmdIsExist("terraform")
if !ok {
return fmt.Errorf("not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ")
}
var err error
tf.setEnv()
defer func() {
tf.unsetEnv()
}()
if !file.IsExist(HomeDir) {
err = fmt.Errorf("infra terraform home dir is not exist")
return err
}
defer func() {
if err == nil {
logger.Warn("destroy error,skip clean files")
file.CleanDir(HomeDir)
}
}()
err = executils.CmdWithContext(context.Background(), "bash", "-c", fmt.Sprintf("cd %s && terraform destroy -auto-approve", HomeDir))
if err != nil {
return err
}
return nil
}

type InfraDetail struct {
}

func (tf *Terraform) Detail() error {
_, ok := executils.CheckCmdIsExist("terraform")
if !ok {
return fmt.Errorf("not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ")
}
var err error
tf.setEnv()
defer func() {
tf.unsetEnv()
}()
if !file.IsExist(path.Join(HomeDir, "terraform.tfstate")) {
err = fmt.Errorf("infra terraform status file is not exist")
return err
}
data, err := file.ReadAll(path.Join(HomeDir, "terraform.tfstate"))
if err != nil {
return err
}
var stateMap map[string]interface{}
if err = json.Unmarshal(data, &stateMap); err != nil {
return err
}
resources, _, _ := unstructured.NestedSlice(stateMap, "resources")
for _, res := range resources {
if obj, ok := res.(map[string]interface{}); ok {
resourceType, _, _ := unstructured.NestedString(obj, "type")
if resourceType == "alicloud_instance" {

}
}
}
return nil
}

func (tf *Terraform) setEnv() {
_ = os.Setenv("ALICLOUD_ACCESS_KEY", tf.accessKey)
_ = os.Setenv("ALICLOUD_SECRET_KEY", tf.secretKey)
}

func (tf *Terraform) unsetEnv() {
_ = os.Unsetenv("ALICLOUD_ACCESS_KEY")
_ = os.Unsetenv("ALICLOUD_SECRET_KEY")
}

func init() {
if HomeDir == "" {
HomeDir = path.Join(homedir.HomeDir(), ".infra", "terraform")
}
}
Loading

0 comments on commit 5896ea0

Please sign in to comment.