Skip to content

Commit bfd5646

Browse files
committed
Merge branch 'master' of /Users/jake/terraform
2 parents 155c8a4 + 2edd06f commit bfd5646

13 files changed

+1644
-0
lines changed

gitlab/config.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gitlab
2+
3+
import (
4+
"github.com/xanzy/go-gitlab"
5+
)
6+
7+
// Config is per-provider, specifies where to connect to gitlab
8+
type Config struct {
9+
Token string
10+
BaseURL string
11+
}
12+
13+
// Client returns a *gitlab.Client to interact with the configured gitlab instance
14+
func (c *Config) Client() (interface{}, error) {
15+
client := gitlab.NewClient(nil, c.Token)
16+
if c.BaseURL != "" {
17+
err := client.SetBaseURL(c.BaseURL)
18+
if err != nil {
19+
// The BaseURL supplied wasn't valid, bail.
20+
return nil, err
21+
}
22+
}
23+
24+
// Test the credentials by checking we can get information about the authenticated user.
25+
_, _, err := client.Users.CurrentUser()
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return client, nil
31+
}

gitlab/provider.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package gitlab
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/schema"
5+
"github.com/hashicorp/terraform/terraform"
6+
)
7+
8+
// Provider returns a terraform.ResourceProvider.
9+
func Provider() terraform.ResourceProvider {
10+
11+
// The actual provider
12+
return &schema.Provider{
13+
Schema: map[string]*schema.Schema{
14+
"token": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
DefaultFunc: schema.EnvDefaultFunc("GITLAB_TOKEN", nil),
18+
Description: descriptions["token"],
19+
},
20+
"base_url": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
DefaultFunc: schema.EnvDefaultFunc("GITLAB_BASE_URL", ""),
24+
Description: descriptions["base_url"],
25+
},
26+
},
27+
ResourcesMap: map[string]*schema.Resource{
28+
"gitlab_group": resourceGitlabGroup(),
29+
"gitlab_project": resourceGitlabProject(),
30+
"gitlab_project_hook": resourceGitlabProjectHook(),
31+
"gitlab_deploy_key": resourceGitlabDeployKey(),
32+
},
33+
34+
ConfigureFunc: providerConfigure,
35+
}
36+
}
37+
38+
var descriptions map[string]string
39+
40+
func init() {
41+
descriptions = map[string]string{
42+
"token": "The OAuth token used to connect to GitLab.",
43+
44+
"base_url": "The GitLab Base API URL",
45+
}
46+
}
47+
48+
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
49+
config := Config{
50+
Token: d.Get("token").(string),
51+
BaseURL: d.Get("base_url").(string),
52+
}
53+
54+
return config.Client()
55+
}

gitlab/provider_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package gitlab
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
var testAccProviders map[string]terraform.ResourceProvider
12+
var testAccProvider *schema.Provider
13+
14+
func init() {
15+
testAccProvider = Provider().(*schema.Provider)
16+
testAccProviders = map[string]terraform.ResourceProvider{
17+
"gitlab": testAccProvider,
18+
}
19+
}
20+
21+
func TestProvider(t *testing.T) {
22+
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
23+
t.Fatalf("err: %s", err)
24+
}
25+
}
26+
27+
func TestProvider_impl(t *testing.T) {
28+
var _ terraform.ResourceProvider = Provider()
29+
}
30+
31+
func testAccPreCheck(t *testing.T) {
32+
if v := os.Getenv("GITLAB_TOKEN"); v == "" {
33+
t.Fatal("GITLAB_TOKEN must be set for acceptance tests")
34+
}
35+
}

gitlab/resource_gitlab_deploy_key.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
func resourceGitlabDeployKey() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceGitlabDeployKeyCreate,
15+
Read: resourceGitlabDeployKeyRead,
16+
Delete: resourceGitlabDeployKeyDelete,
17+
18+
Schema: map[string]*schema.Schema{
19+
"project": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
ForceNew: true,
23+
},
24+
"title": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
ForceNew: true,
28+
},
29+
"key": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
ForceNew: true,
33+
},
34+
"can_push": {
35+
Type: schema.TypeBool,
36+
Optional: true,
37+
Default: false,
38+
ForceNew: true,
39+
},
40+
},
41+
}
42+
}
43+
44+
func resourceGitlabDeployKeyCreate(d *schema.ResourceData, meta interface{}) error {
45+
client := meta.(*gitlab.Client)
46+
project := d.Get("project").(string)
47+
options := &gitlab.AddDeployKeyOptions{
48+
Title: gitlab.String(d.Get("title").(string)),
49+
Key: gitlab.String(d.Get("key").(string)),
50+
CanPush: gitlab.Bool(d.Get("can_push").(bool)),
51+
}
52+
53+
log.Printf("[DEBUG] create gitlab deployment key %s", *options.Title)
54+
55+
deployKey, _, err := client.DeployKeys.AddDeployKey(project, options)
56+
if err != nil {
57+
return err
58+
}
59+
60+
d.SetId(fmt.Sprintf("%d", deployKey.ID))
61+
62+
return resourceGitlabDeployKeyRead(d, meta)
63+
}
64+
65+
func resourceGitlabDeployKeyRead(d *schema.ResourceData, meta interface{}) error {
66+
client := meta.(*gitlab.Client)
67+
project := d.Get("project").(string)
68+
deployKeyID, err := strconv.Atoi(d.Id())
69+
if err != nil {
70+
return err
71+
}
72+
log.Printf("[DEBUG] read gitlab deploy key %s/%d", project, deployKeyID)
73+
74+
deployKey, response, err := client.DeployKeys.GetDeployKey(project, deployKeyID)
75+
if err != nil {
76+
if response.StatusCode == 404 {
77+
log.Printf("[WARN] removing deploy key %d from state because it no longer exists in gitlab", deployKeyID)
78+
d.SetId("")
79+
return nil
80+
}
81+
82+
return err
83+
}
84+
85+
d.Set("title", deployKey.Title)
86+
d.Set("key", deployKey.Key)
87+
d.Set("can_push", deployKey.CanPush)
88+
return nil
89+
}
90+
91+
func resourceGitlabDeployKeyDelete(d *schema.ResourceData, meta interface{}) error {
92+
client := meta.(*gitlab.Client)
93+
project := d.Get("project").(string)
94+
deployKeyID, err := strconv.Atoi(d.Id())
95+
if err != nil {
96+
return err
97+
}
98+
log.Printf("[DEBUG] Delete gitlab deploy key %s", d.Id())
99+
100+
response, err := client.DeployKeys.DeleteDeployKey(project, deployKeyID)
101+
102+
// HTTP 204 is success with no body
103+
if response.StatusCode == 204 {
104+
return nil
105+
}
106+
return err
107+
}

0 commit comments

Comments
 (0)