Skip to content

Commit 349dfe9

Browse files
authored
Fix single product gen (#16217)
1 parent d4c08d6 commit 349dfe9

File tree

12 files changed

+64
-67
lines changed

12 files changed

+64
-67
lines changed

mmv1/api/metadata/metadata_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"testing"
55

66
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
7+
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
78
"github.com/google/go-cmp/cmp"
89
)
910

1011
func TestFromResource(t *testing.T) {
1112
product := &api.Product{
12-
Name: "Product",
13-
BaseUrl: "https://compute.googleapis.com/beta",
13+
Name: "Product",
14+
Version: &product.Version{
15+
BaseUrl: "https://compute.googleapis.com/beta",
16+
},
1417
}
1518
cases := []struct {
1619
name string

mmv1/api/product.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ type Product struct {
5454
// The API versions of this product
5555
Versions []*product.Version
5656

57-
// The base URL for the service API endpoint
58-
// For example: `https://www.googleapis.com/compute/v1/`
59-
BaseUrl string `yaml:"base_url,omitempty"`
60-
61-
// The validator "relative URI" of a resource, relative to the product
62-
// base URL. Specific to defining the resource as a CAI asset.
63-
CaiBaseUrl string `yaml:"caibaseurl,omitempty"`
64-
6557
// The service name from CAI asset name, e.g. bigtable.googleapis.com.
6658
CaiAssetService string `yaml:"cai_asset_service,omitempty"`
6759

@@ -80,6 +72,9 @@ type Product struct {
8072

8173
ClientName string `yaml:"client_name,omitempty"`
8274

75+
// The version of the product which is currently being generated.
76+
Version *product.Version `yaml:"-"`
77+
8378
// The compiler to generate the downstream files, for example "terraformgoogleconversion-codegen".
8479
Compiler string `yaml:"-"`
8580
}
@@ -234,11 +229,6 @@ func (p *Product) ExistsAtVersion(name string) bool {
234229
return false
235230
}
236231

237-
func (p *Product) SetPropertiesBasedOnVersion(version *product.Version) {
238-
p.BaseUrl = version.BaseUrl
239-
p.CaiBaseUrl = version.CaiBaseUrl
240-
}
241-
242232
func (p *Product) TerraformName() string {
243233
if p.LegacyName != "" {
244234
return google.Underscore(p.LegacyName)
@@ -247,10 +237,10 @@ func (p *Product) TerraformName() string {
247237
}
248238

249239
func (p *Product) ServiceBaseUrl() string {
250-
if p.CaiBaseUrl != "" {
251-
return p.CaiBaseUrl
240+
if p.Version.CaiBaseUrl != "" {
241+
return p.Version.CaiBaseUrl
252242
}
253-
return p.BaseUrl
243+
return p.Version.BaseUrl
254244
}
255245

256246
func (p *Product) ServiceName() string {

mmv1/api/product_test.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,28 @@ func TestProductServiceName(t *testing.T) {
139139
{
140140
description: "standard BaseUrl",
141141
obj: Product{
142-
BaseUrl: "https://abc.googleapis.com/beta/",
142+
Version: &product.Version{
143+
BaseUrl: "https://abc.googleapis.com/beta/",
144+
},
143145
},
144146
expected: "abc.googleapis.com",
145147
},
146148
{
147149
description: "BaseUrl with locational subdomain",
148150
obj: Product{
149-
BaseUrl: "https://{{location}}-abc.googleapis.com/ga/",
151+
Version: &product.Version{
152+
BaseUrl: "https://{{location}}-abc.googleapis.com/ga/",
153+
},
150154
},
151155
expected: "abc.googleapis.com",
152156
},
153157
{
154158
description: "BaseUrl and CaiBaseUrl",
155159
obj: Product{
156-
BaseUrl: "https://abc.googleapis.com/ga/",
157-
CaiBaseUrl: "https://def.googleapis.com/ga/",
160+
Version: &product.Version{
161+
BaseUrl: "https://abc.googleapis.com/ga/",
162+
CaiBaseUrl: "https://def.googleapis.com/ga/",
163+
},
158164
},
159165
expected: "def.googleapis.com",
160166
},
@@ -184,43 +190,55 @@ func TestProductServiceVersion(t *testing.T) {
184190
{
185191
description: "standard BaseUrl",
186192
obj: Product{
187-
BaseUrl: "https://abc.googleapis.com/v1/",
193+
Version: &product.Version{
194+
BaseUrl: "https://abc.googleapis.com/v1/",
195+
},
188196
},
189197
expected: "v1",
190198
},
191199
{
192200
description: "BaseUrl without trailing /",
193201
obj: Product{
194-
BaseUrl: "https://abc.googleapis.com/v1",
202+
Version: &product.Version{
203+
BaseUrl: "https://abc.googleapis.com/v1",
204+
},
195205
},
196206
expected: "v1",
197207
},
198208
{
199209
description: "BaseUrl with version of beta",
200210
obj: Product{
201-
BaseUrl: "https://abc.googleapis.com/beta/",
211+
Version: &product.Version{
212+
BaseUrl: "https://abc.googleapis.com/beta/",
213+
},
202214
},
203215
expected: "beta",
204216
},
205217
{
206218
description: "BaseUrl without valid version",
207219
obj: Product{
208-
BaseUrl: "https://abc.googleapis.com/other/",
220+
Version: &product.Version{
221+
BaseUrl: "https://abc.googleapis.com/other/",
222+
},
209223
},
210224
expected: "",
211225
},
212226
{
213227
description: "BaseUrl with additional value in path",
214228
obj: Product{
215-
BaseUrl: "https://abc.googleapis.com/compute/v1/",
229+
Version: &product.Version{
230+
BaseUrl: "https://abc.googleapis.com/compute/v1/",
231+
},
216232
},
217233
expected: "v1",
218234
},
219235
{
220236
description: "standard BaseUrl",
221237
obj: Product{
222-
BaseUrl: "https://{{location}}-abc.googleapis.com/",
223-
CaiBaseUrl: "https://abc.googleapis.com/v1/",
238+
Version: &product.Version{
239+
BaseUrl: "https://{{location}}-abc.googleapis.com/",
240+
CaiBaseUrl: "https://abc.googleapis.com/v1/",
241+
},
224242
},
225243
expected: "v1",
226244
},

mmv1/api/resource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ func (r *Resource) ExcludeIfNotInVersion(version *product.Version) {
11091109
// In newer resources there is much less standardisation in terms of value.
11101110
// Generally for them though, it's the product.base_url + resource.name
11111111
func (r Resource) SelfLinkUrl() string {
1112-
s := []string{r.ProductMetadata.BaseUrl, r.SelfLinkUri()}
1112+
s := []string{r.ProductMetadata.Version.BaseUrl, r.SelfLinkUri()}
11131113
return strings.Join(s, "")
11141114
}
11151115

@@ -1127,7 +1127,7 @@ func (r Resource) SelfLinkUri() string {
11271127
}
11281128

11291129
func (r Resource) CollectionUrl() string {
1130-
s := []string{r.ProductMetadata.BaseUrl, r.collectionUri()}
1130+
s := []string{r.ProductMetadata.Version.BaseUrl, r.collectionUri()}
11311131
return strings.Join(s, "")
11321132
}
11331133

@@ -1793,7 +1793,7 @@ func (r Resource) IamImportFormat() string {
17931793
importFormat := r.IamImportFormatTemplate()
17941794

17951795
importFormat = regexp.MustCompile(`\{\{%?(\w+)\}\}`).ReplaceAllString(importFormat, "%s")
1796-
return strings.ReplaceAll(importFormat, r.ProductMetadata.BaseUrl, "")
1796+
return strings.ReplaceAll(importFormat, r.ProductMetadata.Version.BaseUrl, "")
17971797
}
17981798

17991799
func (r Resource) IamImportParams() []string {
@@ -1971,7 +1971,7 @@ func (r Resource) ListUrlTemplate() string {
19711971
}
19721972

19731973
func (r Resource) DeleteUrlTemplate() string {
1974-
return fmt.Sprintf("%s%s", r.ProductMetadata.BaseUrl, r.DeleteUri())
1974+
return fmt.Sprintf("%s%s", r.ProductMetadata.Version.BaseUrl, r.DeleteUri())
19751975
}
19761976

19771977
func (r Resource) LastNestedQueryKey() string {

mmv1/loader/loader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ func (l *Loader) LoadProduct(productName string) (*api.Product, error) {
197197
return nil, err
198198
}
199199

200+
p.Version = p.VersionObjOrClosest(l.version)
201+
200202
p.Objects = resources
201203
p.Validate()
202204

mmv1/provider/terraform.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func NewTerraform(product *api.Product, versionName string, startTime time.Time,
6464
templateFS: templateFS,
6565
}
6666

67-
t.Product.SetPropertiesBasedOnVersion(&t.Version)
6867
t.Product.SetCompiler(ProviderName(t))
6968
for _, r := range t.Product.Objects {
7069
r.SetCompiler(ProviderName(t))
@@ -89,7 +88,7 @@ func (t Terraform) Generate(outputFolder, resourceToGenerate string, generateCod
8988

9089
func (t *Terraform) GenerateObjects(outputFolder, resourceToGenerate string, generateCode, generateDocs bool) {
9190
for _, object := range t.Product.Objects {
92-
object.ExcludeIfNotInVersion(&t.Version)
91+
object.ExcludeIfNotInVersion(t.Product.Version)
9392

9493
if resourceToGenerate != "" && object.Name != resourceToGenerate {
9594
log.Printf("Excluding %s per user request", object.Name)
@@ -179,7 +178,7 @@ func (t *Terraform) GenerateResourceTestsLegacy(object api.Resource, templateDat
179178
eligibleExample := false
180179
for _, example := range object.Examples {
181180
if !example.ExcludeTest {
182-
if object.ProductMetadata.VersionObjOrClosest(t.Version.Name).CompareTo(object.ProductMetadata.VersionObjOrClosest(example.MinVersion)) >= 0 {
181+
if object.ProductMetadata.VersionObjOrClosest(t.Product.Version.Name).CompareTo(object.ProductMetadata.VersionObjOrClosest(example.MinVersion)) >= 0 {
183182
eligibleExample = true
184183
break
185184
}
@@ -210,7 +209,7 @@ func (t *Terraform) GenerateResourceTests(object api.Resource, templateData Temp
210209
eligibleSample := false
211210
for _, sample := range object.Samples {
212211
if !sample.ExcludeTest {
213-
if object.ProductMetadata.VersionObjOrClosest(t.Version.Name).CompareTo(object.ProductMetadata.VersionObjOrClosest(sample.MinVersion)) >= 0 {
212+
if object.ProductMetadata.VersionObjOrClosest(t.Product.Version.Name).CompareTo(object.ProductMetadata.VersionObjOrClosest(sample.MinVersion)) >= 0 {
214213
eligibleSample = true
215214
break
216215
}

mmv1/provider/terraform_oics.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ import (
2424
"time"
2525

2626
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
27-
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
2827
)
2928

3029
type TerraformOiCS struct {
3130
TargetVersionName string
3231

33-
Version product.Version
34-
3532
Product *api.Product
3633

3734
StartTime time.Time
@@ -43,13 +40,10 @@ func NewTerraformOiCS(product *api.Product, versionName string, startTime time.T
4340
toics := TerraformOiCS{
4441
Product: product,
4542
TargetVersionName: versionName,
46-
Version: *product.VersionObjOrClosest(versionName),
4743
StartTime: startTime,
4844
templateFS: templateFS,
4945
}
5046

51-
toics.Product.SetPropertiesBasedOnVersion(&toics.Version)
52-
5347
return toics
5448
}
5549

@@ -59,7 +53,7 @@ func (toics TerraformOiCS) Generate(outputFolder, resourceToGenerate string, gen
5953

6054
func (toics TerraformOiCS) GenerateObjects(outputFolder, resourceToGenerate string, generateCode, generateDocs bool) {
6155
for _, object := range toics.Product.Objects {
62-
object.ExcludeIfNotInVersion(&toics.Version)
56+
object.ExcludeIfNotInVersion(toics.Product.Version)
6357

6458
if resourceToGenerate != "" && object.Name != resourceToGenerate {
6559
log.Printf("Excluding %s per user request", object.Name)

mmv1/provider/terraform_tgc.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"golang.org/x/exp/slices"
3232

3333
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
34-
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
3534
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
3635
)
3736

@@ -44,8 +43,6 @@ type TerraformGoogleConversion struct {
4443

4544
TargetVersionName string
4645

47-
Version product.Version
48-
4946
Product *api.Product
5047

5148
StartTime time.Time
@@ -57,12 +54,10 @@ func NewTerraformGoogleConversion(product *api.Product, versionName string, star
5754
t := TerraformGoogleConversion{
5855
Product: product,
5956
TargetVersionName: versionName,
60-
Version: *product.VersionObjOrClosest(versionName),
6157
StartTime: startTime,
6258
templateFS: templateFS,
6359
}
6460

65-
t.Product.SetPropertiesBasedOnVersion(&t.Version)
6661
t.Product.SetCompiler(ProviderName(t))
6762
for _, r := range t.Product.Objects {
6863
r.SetCompiler(ProviderName(t))
@@ -84,7 +79,7 @@ func (tgc TerraformGoogleConversion) Generate(outputFolder, resourceToGenerate s
8479

8580
func (tgc TerraformGoogleConversion) GenerateObjects(outputFolder, resourceToGenerate string, generateCode, generateDocs bool) {
8681
for _, object := range tgc.Product.Objects {
87-
object.ExcludeIfNotInVersion(&tgc.Version)
82+
object.ExcludeIfNotInVersion(tgc.Product.Version)
8883

8984
if resourceToGenerate != "" && object.Name != resourceToGenerate {
9085
log.Printf("Excluding %s per user request", object.Name)

mmv1/provider/terraform_tgc_cai2hcl.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ import (
2121
"time"
2222

2323
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
24-
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
2524
"github.com/otiai10/copy"
2625
)
2726

2827
// Code generator for a library converting GCP CAI objects to Terraform state.
2928
type CaiToTerraformConversion struct {
3029
TargetVersionName string
3130

32-
Version product.Version
33-
3431
Product *api.Product
3532

3633
StartTime time.Time
@@ -42,12 +39,10 @@ func NewCaiToTerraformConversion(product *api.Product, versionName string, start
4239
t := CaiToTerraformConversion{
4340
Product: product,
4441
TargetVersionName: versionName,
45-
Version: *product.VersionObjOrClosest(versionName),
4642
StartTime: startTime,
4743
templateFS: templateFS,
4844
}
4945

50-
t.Product.SetPropertiesBasedOnVersion(&t.Version)
5146
t.Product.SetCompiler(ProviderName(t))
5247
for _, r := range t.Product.Objects {
5348
r.SetCompiler(ProviderName(t))

0 commit comments

Comments
 (0)