rancher_environment: add member support
This commit is contained in:
parent
d2eaa6f8e0
commit
03cabb7b7e
|
@ -1,11 +1,13 @@
|
||||||
package rancher
|
package rancher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/hashcode"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/helper/validation"
|
"github.com/hashicorp/terraform/helper/validation"
|
||||||
|
@ -41,6 +43,28 @@ func resourceRancherEnvironment() *schema.Resource {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
|
"member": &schema.Schema{
|
||||||
|
Type: schema.TypeSet,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Resource{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"external_id_type": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
"external_id": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
"role": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Set: memberHash,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,6 +109,19 @@ func resourceRancherEnvironmentCreate(d *schema.ResourceData, meta interface{})
|
||||||
d.SetId(newEnv.Id)
|
d.SetId(newEnv.Id)
|
||||||
log.Printf("[INFO] Environment ID: %s", d.Id())
|
log.Printf("[INFO] Environment ID: %s", d.Id())
|
||||||
|
|
||||||
|
// Add members
|
||||||
|
envClient, err := meta.(*Config).EnvironmentClient(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
members := d.Get("members").([]interface{})
|
||||||
|
_, err = envClient.Project.ActionSetmembers(&newEnv, &rancherClient.SetProjectMembersInput{
|
||||||
|
Members: members,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return resourceRancherEnvironmentRead(d, meta)
|
return resourceRancherEnvironmentRead(d, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,6 +155,14 @@ func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) er
|
||||||
d.Set("name", env.Name)
|
d.Set("name", env.Name)
|
||||||
d.Set("orchestration", getActiveOrchestration(env))
|
d.Set("orchestration", getActiveOrchestration(env))
|
||||||
|
|
||||||
|
envClient, err := meta.(*Config).EnvironmentClient(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
members, _ := envClient.ProjectMember.List(NewListOpts())
|
||||||
|
|
||||||
|
d.Set("member", normalizeMembers(members.Data))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,6 +193,19 @@ func resourceRancherEnvironmentUpdate(d *schema.ResourceData, meta interface{})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update members
|
||||||
|
envClient, err := meta.(*Config).EnvironmentClient(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
members := d.Get("member").(*schema.Set).List()
|
||||||
|
_, err = envClient.Project.ActionSetmembers(&newEnv, &rancherClient.SetProjectMembersInput{
|
||||||
|
Members: makeProjectMembers(members),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return resourceRancherEnvironmentRead(d, meta)
|
return resourceRancherEnvironmentRead(d, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,6 +261,39 @@ func setOrchestrationFields(orchestration string, data map[string]interface{}) {
|
||||||
data[orch] = true
|
data[orch] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizeMembers(in []rancherClient.ProjectMember) (out []interface{}) {
|
||||||
|
for _, m := range in {
|
||||||
|
mm := map[string]string{
|
||||||
|
"external_id_type": m.ExternalIdType,
|
||||||
|
"external_id": m.ExternalId,
|
||||||
|
"role": m.Role,
|
||||||
|
}
|
||||||
|
out = append(out, mm)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeProjectMembers(in []interface{}) (out []interface{}) {
|
||||||
|
for _, m := range in {
|
||||||
|
mMap := m.(map[string]interface{})
|
||||||
|
mm := rancherClient.ProjectMember{
|
||||||
|
ExternalIdType: mMap["external_id_type"].(string),
|
||||||
|
ExternalId: mMap["external_id"].(string),
|
||||||
|
Role: mMap["role"].(string),
|
||||||
|
}
|
||||||
|
out = append(out, mm)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func memberHash(member interface{}) int {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
m := member.(map[string]interface{})
|
||||||
|
buf.WriteString(fmt.Sprintf("%s-", m["external_id_type"]))
|
||||||
|
buf.WriteString(fmt.Sprintf("%s-", m["external_id"]))
|
||||||
|
return hashcode.String(buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
// EnvironmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
// EnvironmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
||||||
// a Rancher Environment.
|
// a Rancher Environment.
|
||||||
func EnvironmentStateRefreshFunc(client *rancherClient.RancherClient, environmentID string) resource.StateRefreshFunc {
|
func EnvironmentStateRefreshFunc(client *rancherClient.RancherClient, environmentID string) resource.StateRefreshFunc {
|
||||||
|
|
Loading…
Reference in New Issue