Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bazel_dep(name = "rules_go", version = "0.59.0")
bazel_dep(name = "rules_go", version = "0.60.0")
bazel_dep(name = "gazelle", version = "0.47.0")
bazel_dep(name = "bazel_skylib", version = "1.8.2")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.24.0")
Expand Down
17 changes: 17 additions & 0 deletions build_defs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "product",
srcs = [
"product.bzl",
],
visibility = ["//visibility:public"],
)

bzl_library(
name = "resource",
srcs = [
"resource.bzl",
],
visibility = ["//visibility:public"],
)
84 changes: 84 additions & 0 deletions build_defs/product.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo")

def _mm_product_impl(ctx):
return [ProductInfo(
name = ctx.label.name,
yaml = ctx.file.src,
version = ctx.attr.version,
)]

mm_product = rule(
implementation = _mm_product_impl,
attrs = {
"version": attr.string(default="ga", mandatory=False),
"src": attr.label(
allow_single_file = [".yaml"],
mandatory = True,
),
},
)

def _tpg_product_impl(ctx):
product = ctx.attr.product[ProductInfo]
resources = [res[ResourceInfo] for res in ctx.attr.resources]
tpg_resources = [res[TpgResourceInfo] for res in ctx.attr.resources]
operations = [res for res in resources if res.has_operation]
inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources]
outputs = [
ctx.actions.declare_file("{}/product.go".format(product.version)),
]
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "product",
"--provider", "tpg",
"--output", outputs[0].path
],
inputs = depset([i for i in inputs]),
outputs = outputs
)
if operations:
operation_go = ctx.actions.declare_file("{}/{}_operation.go".format(product.version, product.name))
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--resource", operations[0].yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "operation",
"--provider", "tpg",
"--output", operation_go.path
],
inputs = depset([i for i in inputs]),
outputs = [operation_go]
)
outputs.append(operation_go)

return [
ctx.attr.product[ProductInfo],
DefaultInfo(files = depset([out for out in outputs])),
]

tpg_product = rule(
implementation = _tpg_product_impl,
attrs = {
"product": attr.label(
providers = [ProductInfo],
mandatory = True,
),
"resources": attr.label_list(
providers = [ResourceInfo, TpgResourceInfo],
mandatory=True,
),
"_compiler": attr.label(
default = Label("//mmv1/cmd"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
)
27 changes: 27 additions & 0 deletions build_defs/providers.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ProductInfo = provider(
"Provider for Magic Modules products.",
fields = {
"name": "name of the product",
"version": "version of the provider to be generated",
"yaml": "the product.yaml file",
},
)

ResourceInfo = provider(
"Provider for Magic Modules resources.",
fields = {
"name": "name of the resource",
"yaml": "the [resource].yaml input file",
"has_sweeper": "whether a sweeper must be generated",
"has_operation": "whether an operation must be generated",
},
)

TpgResourceInfo = provider(
"Provider for TPG resources.",
fields = {
"src": "the generated go file",
"metadata": "the generated metadata.yaml file",
"sweeper": "the optional generated sweeper file",
},
)
111 changes: 111 additions & 0 deletions build_defs/resource.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo")

def _mm_resource_impl(ctx):
return [
ctx.attr.product[ProductInfo],
ResourceInfo(
name = ctx.label.name,
yaml = ctx.file.src,
has_sweeper = ctx.attr.has_sweeper,
has_operation = ctx.attr.has_operation,
),
DefaultInfo(files = depset([ctx.file.src]))
]

mm_resource = rule(
implementation = _mm_resource_impl,
attrs = {
"src": attr.label(
allow_single_file = [".yaml"],
mandatory = True,
),
"product": attr.label(
providers = [ProductInfo],
mandatory=True,
),
"has_sweeper": attr.bool(default=False, mandatory=False),
"has_operation": attr.bool(default=False, mandatory=False),
},
)

def _tpg_resource_impl(ctx):
product = ctx.attr.resource[ProductInfo]
resource = ctx.attr.resource[ResourceInfo]
resource_name = "resource_{}_{}".format(product.name, resource.name)
inputs = [product.yaml, resource.yaml]
outputs = [
ctx.actions.declare_file("{}/{}.go".format(product.version, resource_name)),
ctx.actions.declare_file("{}/{}_generated_meta.yaml".format(product.version, resource_name)),
]
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--resource", resource.yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "resource",
"--provider", "tpg",
"--output", outputs[0].path
],
inputs = depset([i for i in inputs]),
outputs = [outputs[0]]
)
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--resource", resource.yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "metadata",
"--provider", "tpg",
"--output", outputs[1].path
],
inputs = depset([i for i in inputs]),
outputs = [outputs[1]]
)
if resource.has_sweeper:
sweeper_go = ctx.actions.declare_file("{}/{}_sweeper.go".format(product.version, resource_name))
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--resource", resource.yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "sweeper",
"--provider", "tpg",
"--output", sweeper_go.path
],
inputs = depset([i for i in inputs]),
outputs = [sweeper_go]
)
outputs.append(sweeper_go)
return [
ctx.attr.resource[ProductInfo],
ctx.attr.resource[ResourceInfo],
TpgResourceInfo(
src = outputs[0],
metadata = outputs[1],
),
DefaultInfo(files = depset([out for out in outputs])),
]

tpg_resource = rule(
implementation = _tpg_resource_impl,
attrs = {
"resource": attr.label(
providers = [ResourceInfo, ProductInfo],
allow_single_file = [".yaml"],
mandatory = True,
),
"version": attr.string(default="ga", mandatory=False),
"_compiler": attr.label(
default = Label("//mmv1/cmd"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
)
1 change: 0 additions & 1 deletion mmv1/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ go_library(
"//mmv1/api/resource",
"//mmv1/api/utils",
"//mmv1/google",
"@com_github_golang_glog//:glog",
"@in_gopkg_yaml_v3//:yaml_v3",
"@org_golang_x_exp//slices",
],
Expand Down
30 changes: 30 additions & 0 deletions mmv1/api/metadata/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "metadata",
srcs = [
"field.go",
"metadata.go",
],
importpath = "github.com/GoogleCloudPlatform/magic-modules/mmv1/api/metadata",
visibility = ["//visibility:public"],
deps = [
"//mmv1/api",
"//mmv1/google",
],
)

go_test(
name = "metadata_test",
srcs = [
"field_test.go",
"metadata_test.go",
],
embed = [":metadata"],
deps = [
"//mmv1/api",
"//mmv1/api/product",
"//mmv1/google",
"@com_github_google_go_cmp//cmp",
],
)
20 changes: 20 additions & 0 deletions mmv1/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "cmd_lib",
data = ["//mmv1/templates"], # keep
srcs = ["main.go"],
importpath = "github.com/GoogleCloudPlatform/magic-modules/mmv1/cmd",
visibility = ["//visibility:private"],
deps = [
"//mmv1/api",
"//mmv1/provider",
"@rules_go//go/runfiles",
],
)

go_binary(
name = "cmd",
embed = [":cmd_lib"],
visibility = ["//visibility:public"],
)
Loading
Loading