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