• 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

43.14
/internal/provider/data_source_scalr_module_versions.go
1
package provider
2

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

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

13
func dataSourceModuleVersions() *schema.Resource {
114✔
14
        return &schema.Resource{
114✔
15
                Description: "Retrieves a list of module versions by module source or module id.",
114✔
16
                ReadContext: dataSourceModuleVersionsRead,
114✔
17
                Schema: map[string]*schema.Schema{
114✔
18
                        "id": {
114✔
19
                                Description:  "The identifier of а module. Example: `mod-xxxx`",
114✔
20
                                Type:         schema.TypeString,
114✔
21
                                Optional:     true,
114✔
22
                                Computed:     true,
114✔
23
                                AtLeastOneOf: []string{"id", "source"},
114✔
24
                        },
114✔
25
                        "source": {
114✔
26
                                Description: "The source of a module.",
114✔
27
                                Type:        schema.TypeString,
114✔
28
                                Optional:    true,
114✔
29
                                Computed:    true,
114✔
30
                        },
114✔
31
                        "versions": {
114✔
32
                                Description: "The list of semantic versions.",
114✔
33
                                Type:        schema.TypeSet,
114✔
34
                                Computed:    true,
114✔
35
                                Elem: &schema.Resource{
114✔
36
                                        Schema: map[string]*schema.Schema{
114✔
37
                                                "id": {
114✔
38
                                                        Description: "The identifier of а module version. Example: `modver-xxxx`",
114✔
39
                                                        Type:        schema.TypeString,
114✔
40
                                                        Required:    true,
114✔
41
                                                },
114✔
42
                                                "version": {
114✔
43
                                                        Description: "The semantic version. Example: `1.2.3`.",
114✔
44
                                                        Type:        schema.TypeString,
114✔
45
                                                        Required:    true,
114✔
46
                                                },
114✔
47
                                                "status": {
114✔
48
                                                        Description: "The status of a module version. Possible values: `ok`, `pending`, `not_uploaded`, `errored`",
114✔
49
                                                        Type:        schema.TypeString,
114✔
50
                                                        Required:    true,
114✔
51
                                                },
114✔
52
                                        },
114✔
53
                                },
114✔
54
                        },
114✔
55
                }}
114✔
56
}
114✔
57

58
func dataSourceModuleVersionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
×
59
        scalrClient := meta.(*scalr.Client)
×
60

×
61
        moduleID := d.Get("id").(string)
×
62
        moduleSource := d.Get("source").(string)
×
63

×
64
        var module *scalr.Module
×
65

×
66
        if moduleID != "" {
×
67
                log.Printf("[DEBUG] Read module with ID: %s", moduleID)
×
68
                var err error
×
NEW
69
                module, err = scalrClient.Modules.Read(ctx, moduleID, scalr.ModuleReadOptions{})
×
70
                if err != nil {
×
71
                        if errors.Is(err, scalr.ErrResourceNotFound) {
×
72
                                return diag.Errorf("Could not find module with ID '%s'", moduleID)
×
73
                        }
×
74
                        return diag.Errorf("Error retrieving module: %v", err)
×
75
                }
76

77
                if moduleSource == "" {
×
78
                        _ = d.Set("source", module.Source)
×
79
                } else if module.Source != moduleSource {
×
80
                        return diag.Errorf("Could not find module with ID '%s' and source '%s'", moduleID, moduleSource)
×
81
                }
×
82

83
        } else if moduleSource != "" {
×
84
                log.Printf("[DEBUG] Read module with source: %s", moduleSource)
×
85
                var err error
×
86
                module, err = scalrClient.Modules.ReadBySource(ctx, moduleSource)
×
87
                if err != nil {
×
88
                        if errors.Is(err, scalr.ErrResourceNotFound) {
×
89
                                return diag.Errorf("Could not find module with source '%s'", moduleSource)
×
90
                        }
×
91
                        return diag.Errorf("Error retrieving module: %v", err)
×
92
                }
93

94
        } else {
×
95
                return diag.Errorf("Error retrieving module: either 'id' or 'source' is required")
×
96
        }
×
97

98
        log.Printf("[DEBUG] Read versions of module: %s", module.ID)
×
99

×
100
        versions := make([]map[string]interface{}, 0)
×
101
        options := scalr.ModuleVersionListOptions{Module: module.ID}
×
102
        for {
×
103
                page, err := scalrClient.ModuleVersions.List(ctx, options)
×
104
                if err != nil {
×
105
                        return diag.Errorf("Error retrieving versions of module with ID '%s': %v", module.ID, err)
×
106
                }
×
107
                for _, version := range page.Items {
×
108
                        versionItem := map[string]interface{}{
×
109
                                "id":      version.ID,
×
110
                                "version": version.Version,
×
111
                                "status":  string(version.Status),
×
112
                        }
×
113
                        versions = append(versions, versionItem)
×
114
                }
×
115
                if page.CurrentPage >= page.TotalPages {
×
116
                        break
×
117
                }
118
                options.PageNumber = page.NextPage
×
119
        }
120
        _ = d.Set("versions", versions)
×
121

×
122
        d.SetId(module.ID)
×
123
        return nil
×
124
}
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