github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/gitlab/resource_gitlab_group.go (about)

     1  package gitlab
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/helper/validation"
     9  	gitlab "github.com/xanzy/go-gitlab"
    10  )
    11  
    12  func resourceGitlabGroup() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceGitlabGroupCreate,
    15  		Read:   resourceGitlabGroupRead,
    16  		Update: resourceGitlabGroupUpdate,
    17  		Delete: resourceGitlabGroupDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  			"path": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"description": {
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  			},
    32  			"lfs_enabled": {
    33  				Type:     schema.TypeBool,
    34  				Optional: true,
    35  				Default:  true,
    36  			},
    37  			"request_access_enabled": {
    38  				Type:     schema.TypeBool,
    39  				Optional: true,
    40  				Default:  false,
    41  			},
    42  			"visibility_level": {
    43  				Type:         schema.TypeString,
    44  				Optional:     true,
    45  				ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
    46  				Default:      "private",
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceGitlabGroupCreate(d *schema.ResourceData, meta interface{}) error {
    53  	client := meta.(*gitlab.Client)
    54  	options := &gitlab.CreateGroupOptions{
    55  		Name:                 gitlab.String(d.Get("name").(string)),
    56  		LFSEnabled:           gitlab.Bool(d.Get("lfs_enabled").(bool)),
    57  		RequestAccessEnabled: gitlab.Bool(d.Get("request_access_enabled").(bool)),
    58  	}
    59  
    60  	if v, ok := d.GetOk("path"); ok {
    61  		options.Path = gitlab.String(v.(string))
    62  	}
    63  
    64  	if v, ok := d.GetOk("description"); ok {
    65  		options.Description = gitlab.String(v.(string))
    66  	}
    67  
    68  	if v, ok := d.GetOk("visibility_level"); ok {
    69  		options.VisibilityLevel = stringToVisibilityLevel(v.(string))
    70  	}
    71  
    72  	log.Printf("[DEBUG] create gitlab group %q", options.Name)
    73  
    74  	group, _, err := client.Groups.CreateGroup(options)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	d.SetId(fmt.Sprintf("%d", group.ID))
    80  
    81  	return resourceGitlabGroupRead(d, meta)
    82  }
    83  
    84  func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
    85  	client := meta.(*gitlab.Client)
    86  	log.Printf("[DEBUG] read gitlab group %s", d.Id())
    87  
    88  	group, response, err := client.Groups.GetGroup(d.Id())
    89  	if err != nil {
    90  		if response.StatusCode == 404 {
    91  			log.Printf("[WARN] removing group %s from state because it no longer exists in gitlab", d.Id())
    92  			d.SetId("")
    93  			return nil
    94  		}
    95  
    96  		return err
    97  	}
    98  
    99  	d.Set("name", group.Name)
   100  	d.Set("path", group.Path)
   101  	d.Set("description", group.Description)
   102  	d.Set("lfs_enabled", group.LFSEnabled)
   103  	d.Set("request_access_enabled", group.RequestAccessEnabled)
   104  
   105  	return nil
   106  }
   107  
   108  func resourceGitlabGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   109  	client := meta.(*gitlab.Client)
   110  
   111  	options := &gitlab.UpdateGroupOptions{}
   112  
   113  	if d.HasChange("name") {
   114  		options.Name = gitlab.String(d.Get("name").(string))
   115  	}
   116  
   117  	if d.HasChange("path") {
   118  		options.Path = gitlab.String(d.Get("path").(string))
   119  	}
   120  
   121  	if d.HasChange("description") {
   122  		options.Description = gitlab.String(d.Get("description").(string))
   123  	}
   124  
   125  	if d.HasChange("lfs_enabled") {
   126  		options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
   127  	}
   128  
   129  	if d.HasChange("request_access_enabled") {
   130  		options.RequestAccessEnabled = gitlab.Bool(d.Get("request_access_enabled").(bool))
   131  	}
   132  
   133  	if d.HasChange("visibility_level") {
   134  		options.VisibilityLevel = stringToVisibilityLevel(d.Get("visibility_level").(string))
   135  	}
   136  
   137  	log.Printf("[DEBUG] update gitlab group %s", d.Id())
   138  
   139  	_, _, err := client.Groups.UpdateGroup(d.Id(), options)
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	return resourceGitlabGroupRead(d, meta)
   145  }
   146  
   147  func resourceGitlabGroupDelete(d *schema.ResourceData, meta interface{}) error {
   148  	client := meta.(*gitlab.Client)
   149  	log.Printf("[DEBUG] Delete gitlab group %s", d.Id())
   150  
   151  	_, err := client.Groups.DeleteGroup(d.Id())
   152  	return err
   153  }