
AWS 最受欢迎的云资源在哪里?
Cloud Native
开始之前
Cloud Native
provider 的名称 该 provider 对应的 Terraform Modules 的 URL
执行代码
Cloud Native
import ("encoding/json""fmt""io""log""net/http""os""os/exec""path/filepath""strings""github.com/pkg/errors")type TFDownload struct {Data []DataItem `json:"data"`Included []IncludedItem `json:"included"`}type IncludedItem struct {Id string `json:"id"`Attributes Attributes `json:"attributes"`}type DataItem struct {Attributes Attributes `json:"attributes"`Relationships Relationships `json:"relationships"`}type Relationships struct {LatestVersion RelationshipLatestVersion `json:"latest-version"`}type RelationshipLatestVersion struct {Data RelationshipData `json:"data"`}type RelationshipData struct {Id string `json:"id"`}var errNoVariables = errors.New("failed to find main.tf or variables.tf in Terraform configurations")type Attributes struct {Name string `json:"name"`Downloads int `json:"downloads"`Source string `json:"source"`Description string `json:"description"`Verified bool `json:"verified"`}func main() {if len(os.Args) < 2 {fmt.Println("Please provide the cloud provider name and an official Terraform modules URL")os.Exit(1)}providerName := os.Args[1]terraformModulesUrl := os.Args[2]resp, err := http.Get(terraformModulesUrl)if err != nil {log.Fatal(err)}defer resp.Body.Close()body, err := io.ReadAll(resp.Body)if err != nil {log.Fatal(err)}var modules TFDownloadif err := json.Unmarshal(body, &modules); err != nil {fmt.Println(err.Error())os.Exit(1)}if _, err = os.Stat(providerName); err == nil {if err := os.RemoveAll(providerName); err != nil {log.Fatal(err)}fmt.Printf("Successfully deleted existed directory %s\n", providerName)}if _, err = os.Stat(providerName); os.IsNotExist(err) {if err := os.Mkdir(providerName, 0755); err != nil {if !os.IsExist(err) {log.Fatal(err)}fmt.Printf("Successfully created directory %s\n", providerName)}}for _, module := range modules.Data {var description stringfor _, attr := range modules.Included {if module.Relationships.LatestVersion.Data.Id == attr.Id {description = attr.Attributes.Description}}if description == "" {description = strings.ToUpper(providerName) + " " + strings.Title(module.Attributes.Name)}outputFile := fmt.Sprintf("%s/terraform-%s-%s.yaml", providerName, providerName, module.Attributes.Name)if _, err := os.Stat(outputFile); !os.IsNotExist(err) {continue}if providerName == "aws" && (module.Attributes.Name == "rds" || module.Attributes.Name == "s3-bucket" ||module.Attributes.Name == "subnet" || module.Attributes.Name == "vpc") {continue}if err := generateDefinition(providerName, module.Attributes.Name, module.Attributes.Source, "", description); err != nil {fmt.Println(err.Error())os.Exit(1)}}}func generateDefinition(provider, name, gitURL, path, description string) error {defYaml := filepath.Join(provider, fmt.Sprintf("terraform-%s-%s.yaml", provider, name))cmd := fmt.Sprintf("vela def init %s --type component --provider %s --git %s.git --desc \"%s\" -o %s",name, provider, gitURL, description, defYaml)if path != "" {cmd = fmt.Sprintf("%s --path %s", cmd, path)}fmt.Println(cmd)stdout, err := exec.Command("bash", "-c", cmd).CombinedOutput()if err != nil {return errors.Wrap(err, string(stdout))}fmt.Println(string(stdout))return nil}
go run gen.go aws "https://registry.terraform.io/v2/modules?filter%5Bprovider%5D=aws&include=latest-version&page%5Bsize%5D=50&page%5Bnumber%5D=1"
代码简要说明
Cloud Native
{"data": [{"type": "modules","id": "23","attributes": {"downloads": 18440513,"full-name": "terraform-aws-modules/vpc/aws","name": "vpc","namespace": "terraform-aws-modules","owner-name": "","provider-logo-url": "/images/providers/aws.png","provider-name": "aws","source": "https://github.com/terraform-aws-modules/terraform-aws-vpc","verified": true},"relationships": {"latest-version": {"data": {"id": "142143","type": "module-versions"}}},"links": {"self": "/v2/modules/23"}},...],"included": [{"type": "module-versions","id": "36806","attributes": {"created-at": "2020-01-03T11:35:36Z","description": "Terraform module Terraform module for creating AWS IAM Roles with heredocs","downloads": 260030,"published-at": "2020-02-06T06:26:08Z","source": "","tag": "v2.0.0","updated-at": "2022-02-22T00:45:44Z","version": "2.0.0"},"links": {"self": "/v2/module-versions/36806"}},...],...}
data:包含 Modules 名称及属性的列表 Included:筛选出的特定版本的 Modules 具体信息
Name Downloads Source Description Verified
新建目录,生成资源所需文件
生成定义文件
vela def init {ModuleName} --type component --provider {providerName} --git {gitURL} --desc {description} -o {yamlFileName}
gitURL: {Module.Attributes.Source}.git description: 如果 Included 中存在元素 ID 与模块 relationship 中 latest-version 对应 ID 相同,则 description 为 Included 中对应元素属性的 description;否则 description 为 providerName 与模块名称的拼接 yamlFileName:terraform-{providerName}-{Module.Attributes.Name}.yaml
你也来试试?
Cloud Native
相关链接
Cloud Native
[1] 简单快捷的命令行工具
https://kubevela.io/docs/next/platform-engineers/components/component-terraform
[2] AWS 的云资源 Terraform modules
https://registry.terraform.io/namespaces/terraform-aws-modules
点击“阅读原文”,查看 KubeVela 项目官网。
文章转载自阿里巴巴云原生,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




