|
1 | 1 | package regional |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "strings" |
7 | 8 |
|
| 9 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
8 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
9 | 11 | "github.com/scaleway/scaleway-sdk-go/scw" |
10 | 12 | "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
@@ -85,48 +87,65 @@ func ParseID(regionalID string) (region scw.Region, id string, err error) { |
85 | 87 | } |
86 | 88 |
|
87 | 89 | func ResolveRegionAndID( |
| 90 | + ctx context.Context, |
88 | 91 | d *schema.ResourceData, |
89 | 92 | fallbackDefaultRegion func(*schema.ResourceData) (scw.Region, error), |
90 | 93 | ) (scw.Region, string, error) { |
91 | | - if identity, err := d.Identity(); err == nil && identity != nil { |
| 94 | + identity, err := d.Identity() |
| 95 | + if err != nil { |
| 96 | + tflog.Warn(ctx, fmt.Sprintf("failed to read identity from ResourceData: %v", err)) |
| 97 | + } else if identity != nil { |
92 | 98 | if v := identity.Get("id"); v != nil { |
93 | | - ID, _ := v.(string) |
94 | | - if ID != "" { |
| 99 | + id, _ := v.(string) |
| 100 | + if id != "" { |
95 | 101 | if rv := identity.Get("region"); rv != nil { |
96 | 102 | if rstr, ok := rv.(string); ok && rstr != "" { |
97 | | - return scw.Region(rstr), ID, nil |
| 103 | + return scw.Region(rstr), id, nil |
98 | 104 | } |
99 | 105 | } |
100 | 106 |
|
101 | 107 | if sid := d.Id(); sid != "" { |
102 | | - if rFromState, _, err := ParseID(sid); err == nil && rFromState != "" { |
103 | | - return rFromState, ID, nil |
| 108 | + regionFromState, _, err := ParseID(sid) |
| 109 | + if err != nil { |
| 110 | + tflog.Warn(ctx, fmt.Sprintf("failed to parse region from state ID %q: %v", sid, err)) |
| 111 | + } else if regionFromState != "" { |
| 112 | + return regionFromState, id, nil |
104 | 113 | } |
105 | 114 | } |
106 | 115 |
|
107 | 116 | if fallbackDefaultRegion != nil { |
108 | | - if region, err := fallbackDefaultRegion(d); err == nil && region != "" { |
109 | | - return region, ID, nil |
| 117 | + region, err := fallbackDefaultRegion(d) |
| 118 | + if err != nil { |
| 119 | + tflog.Warn(ctx, fmt.Sprintf("fallbackDefaultRegion error for ID %q: %v", id, err)) |
| 120 | + } else if region != "" { |
| 121 | + return region, id, nil |
110 | 122 | } |
111 | 123 | } |
112 | 124 |
|
113 | | - return "", "", fmt.Errorf("cannot resolve region for identity (id=%q)", ID) |
| 125 | + return "", "", fmt.Errorf("cannot resolve region for identity (id=%q)", id) |
114 | 126 | } |
115 | 127 | } |
116 | 128 | } |
117 | 129 |
|
118 | | - if sid := d.Id(); sid != "" { |
119 | | - region, ID, err := ParseID(sid) |
120 | | - if err != nil { |
121 | | - return "", "", err |
122 | | - } |
| 130 | + sid := d.Id() |
| 131 | + if sid == "" { |
| 132 | + tflog.Error(ctx, "cannot resolve identity: both identity.id and state ID are empty") |
123 | 133 |
|
124 | | - if ID == "" { |
125 | | - return "", "", fmt.Errorf("empty id parsed from state ID %q", sid) |
126 | | - } |
| 134 | + return "", "", errors.New("cannot resolve identity: both identity.id and state ID are empty") |
| 135 | + } |
| 136 | + |
| 137 | + region, id, err := ParseID(sid) |
| 138 | + if err != nil { |
| 139 | + tflog.Error(ctx, fmt.Sprintf("failed to parse region/ID from state ID %q: %v", sid, err)) |
| 140 | + |
| 141 | + return "", "", err |
| 142 | + } |
| 143 | + |
| 144 | + if id == "" { |
| 145 | + tflog.Error(ctx, fmt.Sprintf("empty ID parsed from state ID %q", sid)) |
127 | 146 |
|
128 | | - return region, ID, nil |
| 147 | + return "", "", fmt.Errorf("empty ID parsed from state ID %q", sid) |
129 | 148 | } |
130 | 149 |
|
131 | | - return "", "", errors.New("cannot resolve identity: both identity.id and state ID are empty") |
| 150 | + return region, id, nil |
132 | 151 | } |
0 commit comments