• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Scalr / terraform-provider-scalr / 16519773832

25 Jul 2025 10:26AM UTC coverage: 63.857%. First build
16519773832

Pull #441

github

DayS1eeper
SCALRCORE-35309
Pull Request #441: SCALRCORE-35309 Provider > Namespaces support.

242 of 444 new or added lines in 6 files covered. (54.5%)

8749 of 13701 relevant lines covered (63.86%)

301.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.82
/internal/provider/resource_scalr_module.go
1
package provider
2

3
import (
4
        "context"
5
        "errors"
6
        "log"
7

8
        "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9

10
        "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
        "github.com/scalr/go-scalr"
12
)
13

14
func resourceScalrModule() *schema.Resource {
114✔
15
        return &schema.Resource{
114✔
16
                Description:   "Manages the state of a module in the Private Modules Registry. Create and destroy operations are available only.",
114✔
17
                CreateContext: resourceScalrModuleCreate,
114✔
18
                ReadContext:   resourceScalrModuleRead,
114✔
19
                DeleteContext: resourceScalrModuleDelete,
114✔
20
                Importer: &schema.ResourceImporter{
114✔
21
                        StateContext: schema.ImportStatePassthroughContext,
114✔
22
                },
114✔
23
                Schema: map[string]*schema.Schema{
114✔
24
                        "name": {
114✔
25
                                Description: "Name of the module, e.g. `rds`, `compute`, `kubernetes-engine`.",
114✔
26
                                Type:        schema.TypeString,
114✔
27
                                Computed:    true,
114✔
28
                        },
114✔
29
                        "module_provider": {
114✔
30
                                Description: "Module provider name, e.g `aws`, `azurerm`, `google`, etc.",
114✔
31
                                Type:        schema.TypeString,
114✔
32
                                Computed:    true,
114✔
33
                        },
114✔
34
                        "status": {
114✔
35
                                Description: "A system status of the Module.",
114✔
36
                                Type:        schema.TypeString,
114✔
37
                                Computed:    true,
114✔
38
                        },
114✔
39
                        "source": {
114✔
40
                                Description: "The source of a remote module in the private registry, e.g `env-xxxx/aws/vpc`.",
114✔
41
                                Type:        schema.TypeString,
114✔
42
                                Computed:    true,
114✔
43
                        },
114✔
44
                        "vcs_repo": {
114✔
45
                                Description: "Source configuration of a VCS repository.",
114✔
46
                                Type:        schema.TypeList,
114✔
47
                                Required:    true,
114✔
48
                                MinItems:    1,
114✔
49
                                MaxItems:    1,
114✔
50
                                ForceNew:    true,
114✔
51
                                Elem: &schema.Resource{
114✔
52
                                        Schema: map[string]*schema.Schema{
114✔
53
                                                "identifier": {
114✔
54
                                                        Description: "The identifier of a VCS repository in the format `:org/:repo` (`:org/:project/:name` is used for Azure DevOps). It refers to an organization and a repository name in a VCS provider.",
114✔
55
                                                        Type:        schema.TypeString,
114✔
56
                                                        Required:    true,
114✔
57
                                                        ForceNew:    true,
114✔
58
                                                },
114✔
59
                                                "path": {
114✔
60
                                                        Description: "The path to the root module folder. It is expected to have the format `<path>/terraform-<provider_name>-<module_name>`, where `<path>` stands for any folder within the repository inclusively a repository root.",
114✔
61
                                                        Type:        schema.TypeString,
114✔
62
                                                        Optional:    true,
114✔
63
                                                        ForceNew:    true,
114✔
64
                                                },
114✔
65
                                                "tag_prefix": {
114✔
66
                                                        Description: "Registry ignores tags which do not match specified prefix, e.g. `aws/`.",
114✔
67
                                                        Type:        schema.TypeString,
114✔
68
                                                        Optional:    true,
114✔
69
                                                        ForceNew:    true,
114✔
70
                                                },
114✔
71
                                        },
114✔
72
                                },
114✔
73
                        },
114✔
74
                        "vcs_provider_id": {
114✔
75
                                Description: "The identifier of a VCS provider in the format `vcs-<RANDOM STRING>`.",
114✔
76
                                Type:        schema.TypeString,
114✔
77
                                Required:    true,
114✔
78
                                ForceNew:    true,
114✔
79
                        },
114✔
80
                        "account_id": {
114✔
81
                                Description: "The identifier of the account in the format `acc-<RANDOM STRING>`. If it is not specified the module will be registered globally and available across the whole installation. **Deprecated:** Use `namespace_id` instead.",
114✔
82
                                Type:        schema.TypeString,
114✔
83
                                Optional:    true,
114✔
84
                                Computed:    true,
114✔
85
                                Deprecated:  "Use namespace_id instead",
114✔
86
                        },
114✔
87
                        "environment_id": {
114✔
88
                                Description: "The identifier of an environment in the format `env-<RANDOM STRING>`. If it is not specified the module will be registered at the account level and available across all environments within the account specified in `account_id` attribute. **Deprecated:** Use `namespace_id` instead.",
114✔
89
                                Type:        schema.TypeString,
114✔
90
                                ForceNew:    true,
114✔
91
                                Optional:    true,
114✔
92
                                Deprecated:  "Use namespace_id instead",
114✔
93
                        },
114✔
94
                        "namespace_id": {
114✔
95
                                Description:   "The identifier of a module namespace in the format `modns-<RANDOM STRING>`. If specified, the module will be registered in this namespace. Conflicts with `environment_id`.",
114✔
96
                                Type:          schema.TypeString,
114✔
97
                                Optional:      true,
114✔
98
                                Computed:      true,
114✔
99
                                ForceNew:      true,
114✔
100
                                ConflictsWith: []string{"environment_id"},
114✔
101
                        },
114✔
102
                },
114✔
103
        }
114✔
104
}
114✔
105

106
func resourceScalrModuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
×
107
        scalrClient := meta.(*scalr.Client)
×
108

×
109
        vcsRepo := d.Get("vcs_repo").([]interface{})[0].(map[string]interface{})
×
110
        vcsOpt := &scalr.ModuleVCSRepo{
×
111
                Identifier: vcsRepo["identifier"].(string),
×
112
        }
×
113
        if path, ok := vcsRepo["path"].(string); ok && path != "" {
×
114
                vcsOpt.Path = ptr(path)
×
115
        }
×
116
        if prefix, ok := vcsRepo["tag_prefix"].(string); ok && prefix != "" {
×
117
                vcsOpt.TagPrefix = ptr(prefix)
×
118
        }
×
119

120
        opt := scalr.ModuleCreateOptions{
×
121
                VCSRepo:     vcsOpt,
×
122
                VcsProvider: &scalr.VcsProvider{ID: d.Get("vcs_provider_id").(string)},
×
123
        }
×
124

×
125
        if envID, ok := d.GetOk("environment_id"); ok {
×
126
                opt.Environment = &scalr.Environment{ID: envID.(string)}
×
127
        }
×
128

NEW
129
        if namespaceID, ok := d.GetOk("namespace_id"); ok {
×
NEW
130
                opt.Namespace = &scalr.ModuleNamespace{ID: namespaceID.(string)}
×
NEW
131
        }
×
132

133
        m, err := scalrClient.Modules.Create(ctx, opt)
×
134
        if err != nil {
×
135
                return diag.Errorf("Error creating module: %v", err)
×
136
        }
×
137

138
        d.SetId(m.ID)
×
139
        return resourceScalrModuleRead(ctx, d, meta)
×
140
}
141

142
func resourceScalrModuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
×
143
        scalrClient := meta.(*scalr.Client)
×
144
        id := d.Id()
×
145
        log.Printf("[DEBUG] Read configuration of module: %s", id)
×
NEW
146
        m, err := scalrClient.Modules.Read(ctx, id, scalr.ModuleReadOptions{})
×
147
        if err != nil {
×
148
                if errors.Is(err, scalr.ErrResourceNotFound) {
×
149
                        log.Printf("[DEBUG] Module %s no longer exists", id)
×
150
                        d.SetId("")
×
151
                        return nil
×
152
                }
×
153
                return diag.Errorf("Error reading configuration of module %s: %v", id, err)
×
154
        }
155

156
        // Update the config.
157
        _ = d.Set("name", m.Name)
×
158
        _ = d.Set("module_provider", m.Provider)
×
159
        _ = d.Set("status", m.Status)
×
160
        _ = d.Set("source", m.Source)
×
161
        _ = d.Set("vcs_repo", []map[string]interface{}{{
×
162
                "identifier": m.VCSRepo.Identifier,
×
163
                "path":       m.VCSRepo.Path,
×
164
                "tag_prefix": m.VCSRepo.TagPrefix,
×
165
        }})
×
166
        _ = d.Set("vcs_provider_id", m.VcsProvider.ID)
×
167

×
168
        if m.Account != nil {
×
169
                _ = d.Set("account_id", m.Account.ID)
×
170
        }
×
171
        if m.Environment != nil {
×
172
                _ = d.Set("environment_id", m.Environment.ID)
×
173
        }
×
NEW
174
        if m.Namespace != nil {
×
NEW
175
                _ = d.Set("namespace_id", m.Namespace.ID)
×
NEW
176
        }
×
177

178
        return nil
×
179
}
180

181
func resourceScalrModuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
×
182
        scalrClient := meta.(*scalr.Client)
×
183
        id := d.Id()
×
184

×
185
        log.Printf("[DEBUG] Delete module %s", id)
×
186
        err := scalrClient.Modules.Delete(ctx, id)
×
187
        if err != nil {
×
188
                if errors.Is(err, scalr.ErrResourceNotFound) {
×
189
                        return nil
×
190
                }
×
191
                return diag.Errorf("Error deleting module %s: %v", id, err)
×
192
        }
193

194
        return nil
×
195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc