github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/bitbucket/resource_repository.go (about)

     1  package bitbucket
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  type CloneUrl struct {
    13  	Href string `json:"href,omitempty"`
    14  	Name string `json:"name,omitempty"`
    15  }
    16  
    17  type Repository struct {
    18  	SCM         string `json:"scm,omitempty"`
    19  	HasWiki     bool   `json:"has_wiki,omitempty"`
    20  	HasIssues   bool   `json:"has_issues,omitempty"`
    21  	Website     string `json:"website,omitempty"`
    22  	IsPrivate   bool   `json:"is_private,omitempty"`
    23  	ForkPolicy  string `json:"fork_policy,omitempty"`
    24  	Language    string `json:"language,omitempty"`
    25  	Description string `json:"description,omitempty"`
    26  	Name        string `json:"name,omitempty"`
    27  	UUID        string `json:"uuid,omitempty"`
    28  	Project     struct {
    29  		Key string `json:"key,omitempty"`
    30  	} `json:"project,omitempty"`
    31  	Links struct {
    32  		Clone []CloneUrl `json:"clone,omitempty"`
    33  	} `json:"links,omitempty"`
    34  }
    35  
    36  func resourceRepository() *schema.Resource {
    37  	return &schema.Resource{
    38  		Create: resourceRepositoryCreate,
    39  		Update: resourceRepositoryUpdate,
    40  		Read:   resourceRepositoryRead,
    41  		Delete: resourceRepositoryDelete,
    42  		Importer: &schema.ResourceImporter{
    43  			State: schema.ImportStatePassthrough,
    44  		},
    45  
    46  		Schema: map[string]*schema.Schema{
    47  			"scm": &schema.Schema{
    48  				Type:     schema.TypeString,
    49  				Optional: true,
    50  				Default:  "git",
    51  			},
    52  			"has_wiki": &schema.Schema{
    53  				Type:     schema.TypeBool,
    54  				Optional: true,
    55  				Default:  false,
    56  			},
    57  			"has_issues": &schema.Schema{
    58  				Type:     schema.TypeBool,
    59  				Optional: true,
    60  				Default:  false,
    61  			},
    62  			"website": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Optional: true,
    65  			},
    66  			"clone_ssh": &schema.Schema{
    67  				Type:     schema.TypeString,
    68  				Computed: true,
    69  			},
    70  			"clone_https": &schema.Schema{
    71  				Type:     schema.TypeString,
    72  				Computed: true,
    73  			},
    74  			"project_key": &schema.Schema{
    75  				Type:     schema.TypeString,
    76  				Optional: true,
    77  			},
    78  			"is_private": &schema.Schema{
    79  				Type:     schema.TypeBool,
    80  				Optional: true,
    81  				Default:  true,
    82  			},
    83  			"fork_policy": &schema.Schema{
    84  				Type:     schema.TypeString,
    85  				Optional: true,
    86  				Default:  "allow_forks",
    87  			},
    88  			"language": &schema.Schema{
    89  				Type:     schema.TypeString,
    90  				Optional: true,
    91  			},
    92  			"description": &schema.Schema{
    93  				Type:     schema.TypeString,
    94  				Optional: true,
    95  			},
    96  			"owner": &schema.Schema{
    97  				Type:     schema.TypeString,
    98  				Required: true,
    99  			},
   100  			"name": &schema.Schema{
   101  				Type:     schema.TypeString,
   102  				Required: true,
   103  			},
   104  		},
   105  	}
   106  }
   107  
   108  func newRepositoryFromResource(d *schema.ResourceData) *Repository {
   109  	repo := &Repository{
   110  		Name:        d.Get("name").(string),
   111  		Language:    d.Get("language").(string),
   112  		IsPrivate:   d.Get("is_private").(bool),
   113  		Description: d.Get("description").(string),
   114  		ForkPolicy:  d.Get("fork_policy").(string),
   115  		HasWiki:     d.Get("has_wiki").(bool),
   116  		HasIssues:   d.Get("has_issues").(bool),
   117  		SCM:         d.Get("scm").(string),
   118  		Website:     d.Get("website").(string),
   119  	}
   120  
   121  	repo.Project.Key = d.Get("project_key").(string)
   122  	return repo
   123  }
   124  
   125  func resourceRepositoryUpdate(d *schema.ResourceData, m interface{}) error {
   126  	client := m.(*BitbucketClient)
   127  	repository := newRepositoryFromResource(d)
   128  
   129  	var jsonbuffer []byte
   130  
   131  	jsonpayload := bytes.NewBuffer(jsonbuffer)
   132  	enc := json.NewEncoder(jsonpayload)
   133  	enc.Encode(repository)
   134  
   135  	_, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s",
   136  		d.Get("owner").(string),
   137  		d.Get("name").(string),
   138  	), jsonpayload)
   139  
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	return resourceRepositoryRead(d, m)
   145  }
   146  
   147  func resourceRepositoryCreate(d *schema.ResourceData, m interface{}) error {
   148  	client := m.(*BitbucketClient)
   149  	repo := newRepositoryFromResource(d)
   150  
   151  	bytedata, err := json.Marshal(repo)
   152  
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	_, err = client.Post(fmt.Sprintf("2.0/repositories/%s/%s",
   158  		d.Get("owner").(string),
   159  		d.Get("name").(string),
   160  	), bytes.NewBuffer(bytedata))
   161  
   162  	if err != nil {
   163  		return err
   164  	}
   165  
   166  	d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), d.Get("name").(string))))
   167  
   168  	return resourceRepositoryRead(d, m)
   169  }
   170  func resourceRepositoryRead(d *schema.ResourceData, m interface{}) error {
   171  
   172  	client := m.(*BitbucketClient)
   173  	repo_req, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/%s",
   174  		d.Get("owner").(string),
   175  		d.Get("name").(string),
   176  	))
   177  
   178  	if repo_req.StatusCode == 200 {
   179  
   180  		var repo Repository
   181  
   182  		body, readerr := ioutil.ReadAll(repo_req.Body)
   183  		if readerr != nil {
   184  			return readerr
   185  		}
   186  
   187  		decodeerr := json.Unmarshal(body, &repo)
   188  		if decodeerr != nil {
   189  			return decodeerr
   190  		}
   191  
   192  		d.Set("scm", repo.SCM)
   193  		d.Set("is_private", repo.IsPrivate)
   194  		d.Set("has_wiki", repo.HasWiki)
   195  		d.Set("has_issues", repo.HasIssues)
   196  		d.Set("name", repo.Name)
   197  		d.Set("language", repo.Language)
   198  		d.Set("fork_policy", repo.ForkPolicy)
   199  		d.Set("website", repo.Website)
   200  		d.Set("description", repo.Description)
   201  		d.Set("project_key", repo.Project.Key)
   202  
   203  		for _, clone_url := range repo.Links.Clone {
   204  			if clone_url.Name == "https" {
   205  				d.Set("clone_https", clone_url.Href)
   206  			} else {
   207  				d.Set("clone_ssh", clone_url.Href)
   208  			}
   209  		}
   210  	}
   211  
   212  	return nil
   213  }
   214  
   215  func resourceRepositoryDelete(d *schema.ResourceData, m interface{}) error {
   216  	client := m.(*BitbucketClient)
   217  	_, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s",
   218  		d.Get("owner").(string),
   219  		d.Get("name").(string),
   220  	))
   221  
   222  	return err
   223  }