第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Terraform 自定義提供程序 - 數(shù)據(jù)源架構(gòu)

Terraform 自定義提供程序 - 數(shù)據(jù)源架構(gòu)

Go
翻過(guò)高山走不出你 2022-12-05 17:30:02
我正在使用 terraform sdk 創(chuàng)建自定義 terraform 提供程序。我正在嘗試從現(xiàn)有的 API GET 調(diào)用中讀取數(shù)據(jù)。我發(fā)現(xiàn)很難將來(lái)自 API 的 JSON 響應(yīng)映射到 Terraform 模式。這是我的數(shù)據(jù)源模式:func dataSourceProjects() *schema.Resource {  return &schema.Resource{    ReadContext: dataSourceProjectsRead,    Schema: map[string]*schema.Schema{      "members": &schema.Schema{        Type:     schema.TypeList,        Elem:     &schema.Schema{Type: schema.TypeString},        Computed: true,      },      "owners": &schema.Schema{        Type:     schema.TypeList,        Elem:     &schema.Schema{Type: schema.TypeString},        Computed: true,      },    },  }}這是 API JSON 響應(yīng):{  "members": [    "test12",    "test8800",    "test0032",    "test1234"  ],  "owners": [    "test000",    "test111",    "test12",    "test1234"  ]}這是我的數(shù)據(jù)源讀取功能func dataSourceProjectsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {  client := &http.Client{Timeout: 10 * time.Second}  // Warning or errors can be collected in a slice type  var diags diag.Diagnostics  req, err := http.NewRequest("GET", fmt.Sprintf("%s/test/team", "https://myurl/v1"), nil)  req.Header.Add("Authorization", "Bearer xxxxx")  if err != nil {    return diag.FromErr(err)  }  r, err := client.Do(req)  if err != nil {    return diag.FromErr(err)  }  defer r.Body.Close()  members := make([]string, 0)  err = json.NewDecoder(r.Body).Decode(&members)  if err != nil {    return diag.FromErr(err)  }  if err := d.Set("members", members); err != nil {    return diag.FromErr(err)  }  // always run  d.SetId(strconv.FormatInt(time.Now().Unix(), 10))  return diags}我不斷收到此錯(cuò)誤:錯(cuò)誤:json:無(wú)法將對(duì)象解組為 []string 類型的 Go 值
查看完整描述

2 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

服務(wù)器.go


package main


import (

    "log"

    "net/http"

)


func main() {

    s := `

    {

      "members": [

        "test12",

        "test8800",

        "test0032",

        "test1234"

      ],

      "owners": [

        "test000",

        "test111",

        "test12",

        "test1234"

      ]

    }

    `


    http.HandleFunc("/projects", func(w http.ResponseWriter, _ *http.Request) {

      log.Println("Getting Projects")

      w.WriteHeader(http.StatusOK)

      w.Write([]byte(s))

    })


    log.Println("Listening...")

    log.Fatal(http.ListenAndServe(":8000", nil))

}

data_source_projects.go


package hashicups


import (

    "context"

    "encoding/json"

    "fmt"

    "net/http"

    "strconv"

    "time"


    "github.com/hashicorp/terraform-plugin-sdk/v2/diag"

    "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

)


func dataSourceProjects() *schema.Resource {

  return &schema.Resource{

    ReadContext: dataSourceProjectsRead,

    Schema: map[string]*schema.Schema{

      "members": &schema.Schema{

        Type:     schema.TypeList,

        Elem:     &schema.Schema{Type: schema.TypeString},

        Computed: true,

      },

      "owners": &schema.Schema{

        Type:     schema.TypeList,

        Elem:     &schema.Schema{Type: schema.TypeString},

        Computed: true,

      },

    },

  }

}


func dataSourceProjectsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

    client := &http.Client{Timeout: 10 * time.Second}


    // Warning or errors can be collected in a slice type

    var diags diag.Diagnostics


    req, err := http.NewRequest("GET", fmt.Sprintf("%s/projects", "http://localhost:8000"), nil)

    if err != nil {

        return diag.FromErr(err)

    }


    r, err := client.Do(req)

    if err != nil {

        return diag.FromErr(err)

    }

    defer r.Body.Close()


    var projects map[string]interface{}

    err = json.NewDecoder(r.Body).Decode(&projects)

    if err != nil {

        return diag.FromErr(err)

    }


    if err := d.Set("members", projects["members"]); err != nil {

        return diag.FromErr(err)

    }


    if err := d.Set("owners", projects["owners"]); err != nil {

        return diag.FromErr(err)

    }


    // always run

    d.SetId(strconv.FormatInt(time.Now().Unix(), 10))


    return diags

}

輸出:


Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.


  Enter a value: yes



Apply complete! Resources: 0 added, 0 changed, 0 destroyed.


Outputs:


project = {

  "id" = "1651575329"

  "members" = tolist([

    "test12",

    "test8800",

    "test0032",

    "test1234",

  ])

  "owners" = tolist([

    "test000",

    "test111",

    "test12",

    "test1234",

  ])

}


查看完整回答
反對(duì) 回復(fù) 2022-12-05
?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

正如 Matt 和提到的錯(cuò)誤代碼一樣,您正試圖將您的身體解組為不正確的結(jié)構(gòu)。


基本上,您在 Body 中沒(méi)有數(shù)組,您有一個(gè)包含字符串?dāng)?shù)組的字段的對(duì)象。


你的解組方式適用于像這樣的機(jī)構(gòu)


["test12", "test23", "test34"]

嘗試按如下方式對(duì)其進(jìn)行解碼,因此請(qǐng)使用具有顯式映射的結(jié)構(gòu)。


package main


import (

    "encoding/json"

    "fmt"

)


type Schema struct {

    Members []string `json:"members"`

    Owners  []string `json:"owners"`

}


var jsonBody = `{

  "members": [

    "test12",

    "test8800",

    "test0032",

    "test1234"

  ],

  "owners": [

    "test000",

    "test111",

    "test12",

    "test1234"

  ]

}`


func main() {

    var s Schema

    err := json.Unmarshal([]byte(jsonBody), &s)

    if err != nil {

        fmt.Println(err)

    } else {

        fmt.Println(s)

    }


}


查看完整回答
反對(duì) 回復(fù) 2022-12-05
  • 2 回答
  • 0 關(guān)注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)