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