Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/cloud_project_user: Add an importer #220

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions ovh/resource_cloud_project_user.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ovh

import (
"context"
"fmt"
"log"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -22,9 +25,7 @@ func resourceCloudProjectUser() *schema.Resource {
Delete: resourceCloudProjectUserDelete,

Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
},
StateContext: resourceCloudProjectUserImportState,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -105,6 +106,27 @@ func resourceCloudProjectUser() *schema.Resource {
}
}

func resourceCloudProjectUserImportState(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
userId := d.Id()
// Fallback to the environment variable if service_name not given
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE")

splitId := strings.SplitN(userId, "/", 2)
if len(splitId) == 2 {
serviceName = splitId[0]
userId = splitId[1]
}

if serviceName == "" || userId == "" {
return nil, fmt.Errorf("Import Id is not service_name/id formatted")
}

d.SetId(userId)
d.Set("service_name", serviceName)

return []*schema.ResourceData{d}, nil
}

func validateCloudProjectUserRoleFunc(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateStringEnum(v.(string), []string{
"administrator",
Expand Down