| 
 | 1 | +//  | 
 | 2 | +// DISCLAIMER  | 
 | 3 | +//  | 
 | 4 | +// Copyright 2022 ArangoDB GmbH, Cologne, Germany  | 
 | 5 | +//  | 
 | 6 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 7 | +// you may not use this file except in compliance with the License.  | 
 | 8 | +// You may obtain a copy of the License at  | 
 | 9 | +//  | 
 | 10 | +// http://www.apache.org/licenses/LICENSE-2.0  | 
 | 11 | +//  | 
 | 12 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 13 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 15 | +// See the License for the specific language governing permissions and  | 
 | 16 | +// limitations under the License.  | 
 | 17 | +//  | 
 | 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany  | 
 | 19 | +//  | 
 | 20 | + | 
 | 21 | +package provider  | 
 | 22 | + | 
 | 23 | +import (  | 
 | 24 | +	"context"  | 
 | 25 | + | 
 | 26 | +	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"  | 
 | 27 | +	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"  | 
 | 28 | + | 
 | 29 | +	common "github.com/arangodb-managed/apis/common/v1"  | 
 | 30 | +	iam "github.com/arangodb-managed/apis/iam/v1"  | 
 | 31 | +)  | 
 | 32 | + | 
 | 33 | +const (  | 
 | 34 | +	// current user source fields  | 
 | 35 | +	userIdFieldName    = "id"  | 
 | 36 | +	userEmailFieldName = "email"  | 
 | 37 | +	userNameFieldName  = "name"  | 
 | 38 | +)  | 
 | 39 | + | 
 | 40 | +// dataSourceOasisCurrentUser defines a Current User datasource terraform type.  | 
 | 41 | +func dataSourceOasisCurrentUser() *schema.Resource {  | 
 | 42 | +	return &schema.Resource{  | 
 | 43 | +		Description: "Oasis Current User Data Source",  | 
 | 44 | +		ReadContext: dataSourceOasisCurrentUserRead,  | 
 | 45 | + | 
 | 46 | +		Schema: map[string]*schema.Schema{  | 
 | 47 | +			userIdFieldName: {  | 
 | 48 | +				Type:        schema.TypeString,  | 
 | 49 | +				Description: "Current User Data Source User ID field",  | 
 | 50 | +				Optional:    true,  | 
 | 51 | +			},  | 
 | 52 | +			userEmailFieldName: {  | 
 | 53 | +				Type:        schema.TypeString,  | 
 | 54 | +				Description: "Current User Data Source Email field",  | 
 | 55 | +				Optional:    true,  | 
 | 56 | +			},  | 
 | 57 | +			userNameFieldName: {  | 
 | 58 | +				Type:        schema.TypeString,  | 
 | 59 | +				Description: "Current User Data Source Name field",  | 
 | 60 | +				Optional:    true,  | 
 | 61 | +			},  | 
 | 62 | +		},  | 
 | 63 | +	}  | 
 | 64 | +}  | 
 | 65 | + | 
 | 66 | +// dataSourceOasisCurrentUserRead reloads the resource object from the terraform store.  | 
 | 67 | +func dataSourceOasisCurrentUserRead(ctx context.Context, data *schema.ResourceData, m interface{}) diag.Diagnostics {  | 
 | 68 | +	client := m.(*Client)  | 
 | 69 | +	if err := client.Connect(); err != nil {  | 
 | 70 | +		client.log.Error().Err(err).Msg("Failed to connect to api")  | 
 | 71 | +		return diag.FromErr(err)  | 
 | 72 | +	}  | 
 | 73 | + | 
 | 74 | +	iamc := iam.NewIAMServiceClient(client.conn)  | 
 | 75 | +	user, err := iamc.GetThisUser(client.ctxWithToken, &common.Empty{})  | 
 | 76 | +	if err != nil {  | 
 | 77 | +		return diag.FromErr(err)  | 
 | 78 | +	}  | 
 | 79 | + | 
 | 80 | +	for k, v := range flattenCurrentUserObject(user) {  | 
 | 81 | +		if err := data.Set(k, v); err != nil {  | 
 | 82 | +			return diag.FromErr(err)  | 
 | 83 | +		}  | 
 | 84 | +	}  | 
 | 85 | +	data.SetId(user.GetId())  | 
 | 86 | +	return nil  | 
 | 87 | +}  | 
 | 88 | + | 
 | 89 | +// flattenCurrentUserObject creates a map from an Oasis Current User for easy digestion by the terraform schema.  | 
 | 90 | +func flattenCurrentUserObject(user *iam.User) map[string]interface{} {  | 
 | 91 | +	return map[string]interface{}{  | 
 | 92 | +		userIdFieldName:    user.GetId(),  | 
 | 93 | +		userEmailFieldName: user.GetEmail(),  | 
 | 94 | +		userNameFieldName:  user.GetName(),  | 
 | 95 | +	}  | 
 | 96 | +}  | 
0 commit comments