forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_source_repos_repository.go
107 lines (82 loc) · 2.27 KB
/
resource_source_repos_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/sourcerepo/v1"
)
func resourceSourceRepoRepository() *schema.Resource {
return &schema.Resource{
Create: resourceSourceRepoRepositoryCreate,
Read: resourceSourceRepoRepositoryRead,
Delete: resourceSourceRepoRepositoryDelete,
//Update: not supported,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"size": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceSourceRepoRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
repoName := d.Get("name").(string)
name := buildRepositoryName(project, repoName)
repo := &sourcerepo.Repo{
Name: name,
}
parent := "projects/" + project
op, err := config.clientSourceRepo.Projects.Repos.Create(parent, repo).Do()
if err != nil {
return fmt.Errorf("Error creating the Source Repo: %s", err)
}
d.SetId(op.Name)
return nil
}
func resourceSourceRepoRepositoryRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
repoName := d.Get("name").(string)
name := buildRepositoryName(project, repoName)
repo, err := config.clientSourceRepo.Projects.Repos.Get(name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Source Repo %q", d.Id()))
}
d.Set("size", repo.Size)
return nil
}
func resourceSourceRepoRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
repoName := d.Get("name").(string)
name := buildRepositoryName(project, repoName)
_, err = config.clientSourceRepo.Projects.Repos.Delete(name).Do()
if err != nil {
return fmt.Errorf("Error deleting the Source Repo: %s", err)
}
return nil
}
func buildRepositoryName(project, name string) string {
repositoryName := "projects/" + project + "/repos/" + name
return repositoryName
}