Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bitbucket
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "log"
- "io/ioutil"
- "strconv"
- "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
- "net/http"
- "context"
- )
- // Project is the project data we need to send to create a project on the bitbucket api
- type Project struct {
- Key string `json:"key,omitempty"`
- Id int `json:"id,omitempty"`
- Name string `json:"name,omitempty"`
- Public bool `json:"public,omitempty"`
- Type string `json:"type,omitempty"`
- }
- func resourceProject() *schema.Resource {
- return &schema.Resource{
- CreateContext: resourceProjectCreate,
- ReadContext: resourceProjectRead,
- UpdateContext: resourceProjectUpdate,
- DeleteContext: resourceProjectDelete,
- Schema: map[string]*schema.Schema{
- "id": {
- Type: schema.TypeString,
- Computed: true,
- },
- "key": {
- Type: schema.TypeString,
- Required: true,
- },
- "name": {
- Type: schema.TypeString,
- Required: true,
- },
- "public": {
- Type: schema.TypeBool,
- Optional: true,
- },
- "type":{
- Type: schema.TypeString,
- Optional: true,
- },
- },
- }
- }
- // uses project key to get information about a single project resource
- func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
- log.Printf("[DEBUG] starting read with resource:id = %s, key = %s, name = %s, public = %s, type = %s",
- d.Get("id").(string),
- d.Get("key").(string),
- d.Get("name").(string),
- strconv.FormatBool(d.Get("public").(bool)),
- d.Get("type").(string))
- // Warning or errors can be collected in a slice type
- var diags diag.Diagnostics
- var projectKey string
- projectKey = d.Get("key").(string)
- if projectKey == "" {
- projectKey = d.Get("key").(string)
- }
- log.Printf("[DEBUG] Reading project with key %s", projectKey)
- client := m.(*Client)
- r, err := client.Get(fmt.Sprintf("rest/api/1.0/projects/%s",
- projectKey,
- ))
- defer r.Body.Close()
- if err != nil {
- return diag.FromErr(err)
- }
- log.Printf("[DEBUG] read project: id = %s, key = %s, name = %s, public = %s, type = %s",
- d.Get("id").(string),
- d.Get("key").(string),
- d.Get("name").(string),
- strconv.FormatBool(d.Get("public").(bool)),
- d.Get("type").(string))
- return diags
- }
- func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
- // Warning or errors can be collected in a slice type
- var diags diag.Diagnostics
- client := m.(*Client)
- log.Printf("[DEBUG] starting create with resource:id = %s, key = %s, name = %s, public = %s, type = %s",
- d.Get("id").(string),
- d.Get("key").(string),
- d.Get("name").(string),
- strconv.FormatBool(d.Get("public").(bool)),
- d.Get("type").(string))
- project := newProjectFromResource(d)
- log.Printf("[DEBUG] converted resource into project:id = %s, key = %s, name = %s, public = %s, type = %s",
- project.Id,
- project.Key,
- project.Name,
- project.Public,
- project.Type)
- bytedata, err := json.Marshal(project)
- if err != nil {
- return diag.FromErr(err)
- }
- var projectKey string
- projectKey = d.Get("key").(string)
- if projectKey == "" {
- projectKey = d.Get("key").(string)
- }
- projectReq, err := client.Post(fmt.Sprintf("rest/api/1.0/projects/"),
- bytes.NewBuffer(bytedata))
- if err != nil {
- return diag.FromErr(err)
- }
- if projectReq.StatusCode == 201 {
- err, project := parseProjectFromResponse(projectReq)
- if err != nil {
- return diag.FromErr(err)
- }
- log.Printf("[DEBUG] setting ID for newly created project to %s",strconv.Itoa(project.Id))
- d.Set("id", strconv.Itoa(project.Id))
- }
- log.Printf("[DEBUG] created project: id = %s, key = %s, name = %s, public = %s, type = %s",
- d.Get("id").(string),
- d.Get("key").(string),
- d.Get("name").(string),
- strconv.FormatBool(d.Get("public").(bool)),
- d.Get("type").(string))
- //resourceProjectRead(ctx, d, m)
- return diags
- }
- // create a project object from a terraform resource
- func newProjectFromResource(d *schema.ResourceData) *Project {
- project := &Project{
- Name: d.Get("name").(string),
- Public: d.Get("public").(bool),
- Type: d.Get("type").(string),
- Key: d.Get("key").(string),
- }
- return project
- }
- func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
- log.Printf("[DEBUG] running update")
- client := m.(*Client)
- project := newProjectFromResource(d)
- var jsonbuffer []byte
- jsonpayload := bytes.NewBuffer(jsonbuffer)
- enc := json.NewEncoder(jsonpayload)
- enc.Encode(project)
- var projectKey string
- projectKey = d.Get("key").(string)
- if projectKey == "" {
- projectKey = d.Get("key").(string)
- }
- _, err := client.Put(fmt.Sprintf("rest/api/1.0/projects/%s",
- projectKey,
- ), jsonpayload)
- if err != nil {
- return diag.FromErr(err)
- }
- return resourceProjectRead(ctx, d, m)
- }
- // uses project key to delete project
- func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
- log.Printf("[DEBUG] running delete")
- // Warning or errors can be collected in a slice type
- var diags diag.Diagnostics
- var projectKey string
- projectKey = d.Get("key").(string)
- if projectKey == "" {
- projectKey = d.Get("key").(string)
- }
- client := m.(*Client)
- _, err := client.Delete(fmt.Sprintf("rest/api/1.0/projects/%s",
- projectKey,
- ))
- if err != nil {
- return diag.FromErr(err)
- }
- return diags
- }
- func parseProjectFromResponse(r *http.Response) (error, *Project){
- var project *Project
- body, readerr := ioutil.ReadAll(r.Body)
- if readerr != nil {
- return readerr, nil
- }
- decodeerr := json.Unmarshal(body, &project)
- if decodeerr != nil {
- return decodeerr, nil
- }
- return nil, project
- }
Advertisement
Add Comment
Please, Sign In to add comment