github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/github/resource_github_repository.go (about) 1 package github 2 3 import ( 4 "log" 5 6 "github.com/google/go-github/github" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourceGithubRepository() *schema.Resource { 11 12 return &schema.Resource{ 13 Create: resourceGithubRepositoryCreate, 14 Read: resourceGithubRepositoryRead, 15 Update: resourceGithubRepositoryUpdate, 16 Delete: resourceGithubRepositoryDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 "description": &schema.Schema{ 25 Type: schema.TypeString, 26 Optional: true, 27 }, 28 "homepage_url": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 }, 32 "private": &schema.Schema{ 33 Type: schema.TypeBool, 34 Optional: true, 35 }, 36 "has_issues": &schema.Schema{ 37 Type: schema.TypeBool, 38 Optional: true, 39 }, 40 "has_wiki": &schema.Schema{ 41 Type: schema.TypeBool, 42 Optional: true, 43 }, 44 "has_downloads": &schema.Schema{ 45 Type: schema.TypeBool, 46 Optional: true, 47 }, 48 "auto_init": &schema.Schema{ 49 Type: schema.TypeBool, 50 Optional: true, 51 }, 52 53 "full_name": &schema.Schema{ 54 Type: schema.TypeString, 55 Computed: true, 56 }, 57 "default_branch": &schema.Schema{ 58 Type: schema.TypeString, 59 Computed: true, 60 }, 61 "ssh_clone_url": &schema.Schema{ 62 Type: schema.TypeString, 63 Computed: true, 64 }, 65 "svn_url": &schema.Schema{ 66 Type: schema.TypeString, 67 Computed: true, 68 }, 69 "git_clone_url": &schema.Schema{ 70 Type: schema.TypeString, 71 Computed: true, 72 }, 73 "http_clone_url": &schema.Schema{ 74 Type: schema.TypeString, 75 Computed: true, 76 }, 77 }, 78 } 79 } 80 81 func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository { 82 name := d.Get("name").(string) 83 description := d.Get("description").(string) 84 homepageUrl := d.Get("homepage_url").(string) 85 private := d.Get("private").(bool) 86 hasIssues := d.Get("has_issues").(bool) 87 hasWiki := d.Get("has_wiki").(bool) 88 hasDownloads := d.Get("has_downloads").(bool) 89 autoInit := d.Get("auto_init").(bool) 90 91 repo := &github.Repository{ 92 Name: &name, 93 Description: &description, 94 Homepage: &homepageUrl, 95 Private: &private, 96 HasIssues: &hasIssues, 97 HasWiki: &hasWiki, 98 HasDownloads: &hasDownloads, 99 AutoInit: &autoInit, 100 } 101 102 return repo 103 } 104 105 func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) error { 106 client := meta.(*Organization).client 107 108 repoReq := resourceGithubRepositoryObject(d) 109 log.Printf("[DEBUG] create github repository %s/%s", meta.(*Organization).name, *repoReq.Name) 110 repo, _, err := client.Repositories.Create(meta.(*Organization).name, repoReq) 111 if err != nil { 112 return err 113 } 114 d.SetId(*repo.Name) 115 116 return resourceGithubRepositoryRead(d, meta) 117 } 118 119 func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error { 120 client := meta.(*Organization).client 121 repoName := d.Id() 122 123 log.Printf("[DEBUG] read github repository %s/%s", meta.(*Organization).name, repoName) 124 repo, resp, err := client.Repositories.Get(meta.(*Organization).name, repoName) 125 if err != nil { 126 if resp.StatusCode == 404 { 127 log.Printf( 128 "[WARN] removing %s/%s from state because it no longer exists in github", 129 meta.(*Organization).name, 130 repoName, 131 ) 132 d.SetId("") 133 return nil 134 } 135 return err 136 } 137 d.Set("description", repo.Description) 138 d.Set("homepage_url", repo.Homepage) 139 d.Set("private", repo.Private) 140 d.Set("has_issues", repo.HasIssues) 141 d.Set("has_wiki", repo.HasWiki) 142 d.Set("has_downloads", repo.HasDownloads) 143 d.Set("full_name", repo.FullName) 144 d.Set("default_branch", repo.DefaultBranch) 145 d.Set("ssh_clone_url", repo.SSHURL) 146 d.Set("svn_url", repo.SVNURL) 147 d.Set("git_clone_url", repo.GitURL) 148 d.Set("http_clone_url", repo.CloneURL) 149 return nil 150 } 151 152 func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) error { 153 client := meta.(*Organization).client 154 repoReq := resourceGithubRepositoryObject(d) 155 repoName := d.Id() 156 log.Printf("[DEBUG] update github repository %s/%s", meta.(*Organization).name, repoName) 157 _, _, err := client.Repositories.Edit(meta.(*Organization).name, repoName, repoReq) 158 if err != nil { 159 return err 160 } 161 return resourceGithubRepositoryRead(d, meta) 162 } 163 164 func resourceGithubRepositoryDelete(d *schema.ResourceData, meta interface{}) error { 165 client := meta.(*Organization).client 166 repoName := d.Id() 167 log.Printf("[DEBUG] delete github repository %s/%s", meta.(*Organization).name, repoName) 168 _, err := client.Repositories.Delete(meta.(*Organization).name, repoName) 169 return err 170 }