2016-10-11 19:05:38 +02:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2017-03-12 15:05:18 +01:00
|
|
|
"context"
|
2016-10-11 19:05:38 +02:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceGithubRepository() *schema.Resource {
|
|
|
|
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceGithubRepositoryCreate,
|
|
|
|
Read: resourceGithubRepositoryRead,
|
|
|
|
Update: resourceGithubRepositoryUpdate,
|
|
|
|
Delete: resourceGithubRepositoryDelete,
|
2016-11-28 18:30:24 +01:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-10-11 19:05:38 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-12 15:05:18 +01:00
|
|
|
"name": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"description": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"homepage_url": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"private": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"has_issues": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"has_wiki": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"has_downloads": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"auto_init": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-03-12 15:05:18 +01:00
|
|
|
"full_name": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"default_branch": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"ssh_clone_url": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"svn_url": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"git_clone_url": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"http_clone_url": {
|
2016-10-11 19:05:38 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
description := d.Get("description").(string)
|
|
|
|
homepageUrl := d.Get("homepage_url").(string)
|
|
|
|
private := d.Get("private").(bool)
|
|
|
|
hasIssues := d.Get("has_issues").(bool)
|
|
|
|
hasWiki := d.Get("has_wiki").(bool)
|
|
|
|
hasDownloads := d.Get("has_downloads").(bool)
|
|
|
|
autoInit := d.Get("auto_init").(bool)
|
|
|
|
|
|
|
|
repo := &github.Repository{
|
|
|
|
Name: &name,
|
|
|
|
Description: &description,
|
|
|
|
Homepage: &homepageUrl,
|
|
|
|
Private: &private,
|
|
|
|
HasIssues: &hasIssues,
|
|
|
|
HasWiki: &hasWiki,
|
|
|
|
HasDownloads: &hasDownloads,
|
|
|
|
AutoInit: &autoInit,
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*Organization).client
|
|
|
|
|
|
|
|
repoReq := resourceGithubRepositoryObject(d)
|
|
|
|
log.Printf("[DEBUG] create github repository %s/%s", meta.(*Organization).name, *repoReq.Name)
|
2017-03-12 15:05:18 +01:00
|
|
|
repo, _, err := client.Repositories.Create(context.TODO(), meta.(*Organization).name, repoReq)
|
2016-10-11 19:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.SetId(*repo.Name)
|
|
|
|
|
|
|
|
return resourceGithubRepositoryRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*Organization).client
|
|
|
|
repoName := d.Id()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] read github repository %s/%s", meta.(*Organization).name, repoName)
|
2017-03-12 15:05:18 +01:00
|
|
|
repo, resp, err := client.Repositories.Get(context.TODO(), meta.(*Organization).name, repoName)
|
2016-10-11 19:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
log.Printf(
|
|
|
|
"[WARN] removing %s/%s from state because it no longer exists in github",
|
|
|
|
meta.(*Organization).name,
|
|
|
|
repoName,
|
|
|
|
)
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-11-28 18:30:24 +01:00
|
|
|
d.Set("name", repoName)
|
2016-10-11 19:05:38 +02:00
|
|
|
d.Set("description", repo.Description)
|
|
|
|
d.Set("homepage_url", repo.Homepage)
|
|
|
|
d.Set("private", repo.Private)
|
|
|
|
d.Set("has_issues", repo.HasIssues)
|
|
|
|
d.Set("has_wiki", repo.HasWiki)
|
|
|
|
d.Set("has_downloads", repo.HasDownloads)
|
|
|
|
d.Set("full_name", repo.FullName)
|
|
|
|
d.Set("default_branch", repo.DefaultBranch)
|
|
|
|
d.Set("ssh_clone_url", repo.SSHURL)
|
|
|
|
d.Set("svn_url", repo.SVNURL)
|
|
|
|
d.Set("git_clone_url", repo.GitURL)
|
|
|
|
d.Set("http_clone_url", repo.CloneURL)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*Organization).client
|
|
|
|
repoReq := resourceGithubRepositoryObject(d)
|
|
|
|
repoName := d.Id()
|
|
|
|
log.Printf("[DEBUG] update github repository %s/%s", meta.(*Organization).name, repoName)
|
2017-03-12 15:05:18 +01:00
|
|
|
repo, _, err := client.Repositories.Edit(context.TODO(), meta.(*Organization).name, repoName, repoReq)
|
2016-10-11 19:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-28 18:30:24 +01:00
|
|
|
d.SetId(*repo.Name)
|
|
|
|
|
2016-10-11 19:05:38 +02:00
|
|
|
return resourceGithubRepositoryRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*Organization).client
|
|
|
|
repoName := d.Id()
|
|
|
|
log.Printf("[DEBUG] delete github repository %s/%s", meta.(*Organization).name, repoName)
|
2017-03-12 15:05:18 +01:00
|
|
|
_, err := client.Repositories.Delete(context.TODO(), meta.(*Organization).name, repoName)
|
2016-10-11 19:05:38 +02:00
|
|
|
return err
|
|
|
|
}
|