mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 00:56:43 +00:00
197 lines
5.6 KiB
Go
197 lines
5.6 KiB
Go
// ----------------------------------------------------------------------------
|
|
//
|
|
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
|
|
//
|
|
// ----------------------------------------------------------------------------
|
|
//
|
|
// This file is automatically generated by Magic Modules and manual
|
|
// changes will be clobbered when the file is regenerated.
|
|
//
|
|
// Please read more about how to change this file in
|
|
// .github/CONTRIBUTING.md.
|
|
//
|
|
// ----------------------------------------------------------------------------
|
|
|
|
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func resourceSourcerepoRepository() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceSourcerepoRepositoryCreate,
|
|
Read: resourceSourcerepoRepositoryRead,
|
|
Delete: resourceSourcerepoRepositoryDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
State: resourceSourcerepoRepositoryImport,
|
|
},
|
|
|
|
Timeouts: &schema.ResourceTimeout{
|
|
Create: schema.DefaultTimeout(240 * time.Second),
|
|
Delete: schema.DefaultTimeout(240 * time.Second),
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"name": {
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
"size": {
|
|
Type: schema.TypeInt,
|
|
Computed: true,
|
|
},
|
|
"url": {
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"project": {
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceSourcerepoRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
obj := make(map[string]interface{})
|
|
nameProp, err := expandSourcerepoRepositoryName(d.Get("name"), d, config)
|
|
if err != nil {
|
|
return err
|
|
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
|
|
obj["name"] = nameProp
|
|
}
|
|
|
|
url, err := replaceVars(d, config, "https://sourcerepo.googleapis.com/v1/projects/{{project}}/repos")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Printf("[DEBUG] Creating new Repository: %#v", obj)
|
|
res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutCreate))
|
|
if err != nil {
|
|
return fmt.Errorf("Error creating Repository: %s", err)
|
|
}
|
|
|
|
// Store the ID now
|
|
id, err := replaceVars(d, config, "{{project}}/{{name}}")
|
|
if err != nil {
|
|
return fmt.Errorf("Error constructing id: %s", err)
|
|
}
|
|
d.SetId(id)
|
|
|
|
log.Printf("[DEBUG] Finished creating Repository %q: %#v", d.Id(), res)
|
|
|
|
return resourceSourcerepoRepositoryRead(d, meta)
|
|
}
|
|
|
|
func resourceSourcerepoRepositoryRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
url, err := replaceVars(d, config, "https://sourcerepo.googleapis.com/v1/projects/{{project}}/repos/{{name}}")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := sendRequest(config, "GET", url, nil)
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("SourcerepoRepository %q", d.Id()))
|
|
}
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := d.Set("project", project); err != nil {
|
|
return fmt.Errorf("Error reading Repository: %s", err)
|
|
}
|
|
|
|
if err := d.Set("name", flattenSourcerepoRepositoryName(res["name"], d)); err != nil {
|
|
return fmt.Errorf("Error reading Repository: %s", err)
|
|
}
|
|
if err := d.Set("url", flattenSourcerepoRepositoryUrl(res["url"], d)); err != nil {
|
|
return fmt.Errorf("Error reading Repository: %s", err)
|
|
}
|
|
if err := d.Set("size", flattenSourcerepoRepositorySize(res["size"], d)); err != nil {
|
|
return fmt.Errorf("Error reading Repository: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceSourcerepoRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
url, err := replaceVars(d, config, "https://sourcerepo.googleapis.com/v1/projects/{{project}}/repos/{{name}}")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var obj map[string]interface{}
|
|
log.Printf("[DEBUG] Deleting Repository %q", d.Id())
|
|
res, err := sendRequestWithTimeout(config, "DELETE", url, obj, d.Timeout(schema.TimeoutDelete))
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, "Repository")
|
|
}
|
|
|
|
log.Printf("[DEBUG] Finished deleting Repository %q: %#v", d.Id(), res)
|
|
return nil
|
|
}
|
|
|
|
func resourceSourcerepoRepositoryImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
config := meta.(*Config)
|
|
if err := parseImportId([]string{"projects/(?P<project>[^/]+)/repos/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Replace import id for the resource id
|
|
id, err := replaceVars(d, config, "{{project}}/{{name}}")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error constructing id: %s", err)
|
|
}
|
|
d.SetId(id)
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
}
|
|
|
|
func flattenSourcerepoRepositoryName(v interface{}, d *schema.ResourceData) interface{} {
|
|
if v == nil {
|
|
return v
|
|
}
|
|
|
|
// We can't use a standard name_from_self_link because the name can include /'s
|
|
parts := strings.SplitAfterN(v.(string), "/", 4)
|
|
return parts[3]
|
|
}
|
|
|
|
func flattenSourcerepoRepositoryUrl(v interface{}, d *schema.ResourceData) interface{} {
|
|
return v
|
|
}
|
|
|
|
func flattenSourcerepoRepositorySize(v interface{}, d *schema.ResourceData) interface{} {
|
|
// Handles the string fixed64 format
|
|
if strVal, ok := v.(string); ok {
|
|
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
|
|
return intVal
|
|
} // let terraform core handle it if we can't convert the string to an int.
|
|
}
|
|
return v
|
|
}
|
|
|
|
func expandSourcerepoRepositoryName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
|
|
return replaceVars(d, config, "projects/{{project}}/repos/{{name}}")
|
|
}
|