github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/opsgenie/data_source_opsgenie_user.go (about)

     1  package opsgenie
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/opsgenie/opsgenie-go-sdk/user"
     9  )
    10  
    11  func dataSourceOpsGenieUser() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceOpsGenieUserRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"username": {
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  			},
    20  			"full_name": {
    21  				Type:     schema.TypeString,
    22  				Computed: true,
    23  			},
    24  			"role": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func dataSourceOpsGenieUserRead(d *schema.ResourceData, meta interface{}) error {
    33  	client := meta.(*OpsGenieClient).users
    34  
    35  	username := d.Get("username").(string)
    36  
    37  	log.Printf("[INFO] Reading OpsGenie user '%s'", username)
    38  
    39  	o := user.ListUsersRequest{}
    40  	resp, err := client.List(o)
    41  	if err != nil {
    42  		return nil
    43  	}
    44  
    45  	var found *user.GetUserResponse
    46  
    47  	if len(resp.Users) > 0 {
    48  		for _, user := range resp.Users {
    49  			if user.Username == username {
    50  				found = &user
    51  				break
    52  			}
    53  		}
    54  	}
    55  
    56  	if found == nil {
    57  		return fmt.Errorf("Unable to locate any user with the username: %s", username)
    58  	}
    59  
    60  	d.SetId(found.Id)
    61  	d.Set("username", found.Username)
    62  	d.Set("full_name", found.Fullname)
    63  	d.Set("role", found.Role)
    64  
    65  	return nil
    66  }