1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package main
import ( "io" "log" "net/http" "net/url"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/tidwall/gjson" )
func resourceDemoServer() *schema.Resource { return &schema.Resource{ Create: resourceDemoServerCreate, Read: resourceDemoServerRead, Update: resourceDemoServerUpdate, Delete: resourceDemoServerDelete,
Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, Required: true, }, "comment": { Type: schema.TypeString, Required: true, }, }, } }
func resourceDemoServerCreate(d *schema.ResourceData, m interface{}) error { var name string = d.Get("name").(string) var comment string = d.Get("comment").(string)
resp, err := http.PostForm("http://localhost:8080/demo/addDemo.do", url.Values{"formString": {"{'name':'" + name + "','comment':'" + comment + "'}"}}) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } jsonstring := string(body) id := gjson.Get(jsonstring, "data") d.SetId(id.String())
return resourceDemoServerRead(d, m) }
func resourceDemoServerRead(d *schema.ResourceData, m interface{}) error { resp, err := http.Get("http://localhost:8080/demo/getDemoById.do?id=" + d.Id()) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } jsonstring := string(body) name := gjson.Get(jsonstring, "data.name") comment := gjson.Get(jsonstring, "data.comment") d.Set("name", name.String()) d.Set("comment", comment.String()) return nil }
func resourceDemoServerUpdate(d *schema.ResourceData, m interface{}) error { var name string = d.Get("name").(string) var comment string = d.Get("comment").(string) var id string = d.Id()
resp, err := http.PostForm("http://localhost:8080/demo/updateDemo.do", url.Values{"formString": {"{'id':'" + id + "','name':'" + name + "','comment':'" + comment + "'}"}}) if err != nil { log.Fatal(err) } defer resp.Body.Close() return resourceDemoServerRead(d, m) }
func resourceDemoServerDelete(d *schema.ResourceData, m interface{}) error { resp, err := http.Get("http://localhost:8080/demo/deleteDemo.do?id=" + d.Id()) if err != nil { log.Fatal(err) } defer resp.Body.Close() return nil }
|