Guest User

resource_project.go

a guest
Dec 23rd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. package bitbucket
  2.  
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "io/ioutil"
  9. "strconv"
  10.  
  11. "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
  12. "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
  13. "net/http"
  14. "context"
  15. )
  16.  
  17. // Project is the project data we need to send to create a project on the bitbucket api
  18. type Project struct {
  19. Key string `json:"key,omitempty"`
  20. Id int `json:"id,omitempty"`
  21. Name string `json:"name,omitempty"`
  22. Public bool `json:"public,omitempty"`
  23. Type string `json:"type,omitempty"`
  24. }
  25.  
  26. func resourceProject() *schema.Resource {
  27. return &schema.Resource{
  28. CreateContext: resourceProjectCreate,
  29. ReadContext: resourceProjectRead,
  30. UpdateContext: resourceProjectUpdate,
  31. DeleteContext: resourceProjectDelete,
  32. Schema: map[string]*schema.Schema{
  33. "id": {
  34. Type: schema.TypeString,
  35. Computed: true,
  36. },
  37. "key": {
  38. Type: schema.TypeString,
  39. Required: true,
  40. },
  41. "name": {
  42. Type: schema.TypeString,
  43. Required: true,
  44. },
  45. "public": {
  46. Type: schema.TypeBool,
  47. Optional: true,
  48. },
  49. "type":{
  50. Type: schema.TypeString,
  51. Optional: true,
  52. },
  53. },
  54. }
  55. }
  56.  
  57. // uses project key to get information about a single project resource
  58. func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
  59.  
  60. log.Printf("[DEBUG] starting read with resource:id = %s, key = %s, name = %s, public = %s, type = %s",
  61. d.Get("id").(string),
  62. d.Get("key").(string),
  63. d.Get("name").(string),
  64. strconv.FormatBool(d.Get("public").(bool)),
  65. d.Get("type").(string))
  66.  
  67. // Warning or errors can be collected in a slice type
  68. var diags diag.Diagnostics
  69.  
  70. var projectKey string
  71. projectKey = d.Get("key").(string)
  72. if projectKey == "" {
  73. projectKey = d.Get("key").(string)
  74. }
  75.  
  76. log.Printf("[DEBUG] Reading project with key %s", projectKey)
  77. client := m.(*Client)
  78. r, err := client.Get(fmt.Sprintf("rest/api/1.0/projects/%s",
  79. projectKey,
  80. ))
  81.  
  82. defer r.Body.Close()
  83.  
  84. if err != nil {
  85. return diag.FromErr(err)
  86. }
  87.  
  88.  
  89. log.Printf("[DEBUG] read project: id = %s, key = %s, name = %s, public = %s, type = %s",
  90. d.Get("id").(string),
  91. d.Get("key").(string),
  92. d.Get("name").(string),
  93. strconv.FormatBool(d.Get("public").(bool)),
  94. d.Get("type").(string))
  95.  
  96. return diags
  97. }
  98.  
  99. func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
  100.  
  101. // Warning or errors can be collected in a slice type
  102. var diags diag.Diagnostics
  103.  
  104. client := m.(*Client)
  105.  
  106. log.Printf("[DEBUG] starting create with resource:id = %s, key = %s, name = %s, public = %s, type = %s",
  107. d.Get("id").(string),
  108. d.Get("key").(string),
  109. d.Get("name").(string),
  110. strconv.FormatBool(d.Get("public").(bool)),
  111. d.Get("type").(string))
  112.  
  113. project := newProjectFromResource(d)
  114.  
  115. log.Printf("[DEBUG] converted resource into project:id = %s, key = %s, name = %s, public = %s, type = %s",
  116. project.Id,
  117. project.Key,
  118. project.Name,
  119. project.Public,
  120. project.Type)
  121.  
  122. bytedata, err := json.Marshal(project)
  123.  
  124. if err != nil {
  125. return diag.FromErr(err)
  126. }
  127.  
  128. var projectKey string
  129. projectKey = d.Get("key").(string)
  130. if projectKey == "" {
  131. projectKey = d.Get("key").(string)
  132. }
  133.  
  134. projectReq, err := client.Post(fmt.Sprintf("rest/api/1.0/projects/"),
  135. bytes.NewBuffer(bytedata))
  136.  
  137. if err != nil {
  138. return diag.FromErr(err)
  139. }
  140.  
  141. if projectReq.StatusCode == 201 {
  142. err, project := parseProjectFromResponse(projectReq)
  143. if err != nil {
  144. return diag.FromErr(err)
  145. }
  146. log.Printf("[DEBUG] setting ID for newly created project to %s",strconv.Itoa(project.Id))
  147. d.Set("id", strconv.Itoa(project.Id))
  148. }
  149.  
  150. log.Printf("[DEBUG] created project: id = %s, key = %s, name = %s, public = %s, type = %s",
  151. d.Get("id").(string),
  152. d.Get("key").(string),
  153. d.Get("name").(string),
  154. strconv.FormatBool(d.Get("public").(bool)),
  155. d.Get("type").(string))
  156.  
  157. //resourceProjectRead(ctx, d, m)
  158. return diags
  159. }
  160.  
  161. // create a project object from a terraform resource
  162. func newProjectFromResource(d *schema.ResourceData) *Project {
  163. project := &Project{
  164. Name: d.Get("name").(string),
  165. Public: d.Get("public").(bool),
  166. Type: d.Get("type").(string),
  167. Key: d.Get("key").(string),
  168. }
  169.  
  170. return project
  171. }
  172.  
  173. func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
  174.  
  175. log.Printf("[DEBUG] running update")
  176.  
  177. client := m.(*Client)
  178. project := newProjectFromResource(d)
  179.  
  180. var jsonbuffer []byte
  181.  
  182. jsonpayload := bytes.NewBuffer(jsonbuffer)
  183. enc := json.NewEncoder(jsonpayload)
  184. enc.Encode(project)
  185.  
  186. var projectKey string
  187. projectKey = d.Get("key").(string)
  188. if projectKey == "" {
  189. projectKey = d.Get("key").(string)
  190. }
  191.  
  192. _, err := client.Put(fmt.Sprintf("rest/api/1.0/projects/%s",
  193. projectKey,
  194. ), jsonpayload)
  195.  
  196. if err != nil {
  197. return diag.FromErr(err)
  198. }
  199.  
  200. return resourceProjectRead(ctx, d, m)
  201. }
  202.  
  203. // uses project key to delete project
  204. func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
  205.  
  206. log.Printf("[DEBUG] running delete")
  207. // Warning or errors can be collected in a slice type
  208. var diags diag.Diagnostics
  209.  
  210. var projectKey string
  211. projectKey = d.Get("key").(string)
  212. if projectKey == "" {
  213. projectKey = d.Get("key").(string)
  214. }
  215.  
  216. client := m.(*Client)
  217. _, err := client.Delete(fmt.Sprintf("rest/api/1.0/projects/%s",
  218. projectKey,
  219. ))
  220.  
  221. if err != nil {
  222. return diag.FromErr(err)
  223. }
  224.  
  225. return diags
  226. }
  227.  
  228. func parseProjectFromResponse(r *http.Response) (error, *Project){
  229.  
  230. var project *Project
  231.  
  232. body, readerr := ioutil.ReadAll(r.Body)
  233. if readerr != nil {
  234. return readerr, nil
  235. }
  236.  
  237. decodeerr := json.Unmarshal(body, &project)
  238. if decodeerr != nil {
  239. return decodeerr, nil
  240. }
  241.  
  242. return nil, project
  243. }
Advertisement
Add Comment
Please, Sign In to add comment