|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "golang.org/x/text/cases" |
| 12 | + "golang.org/x/text/language" |
| 13 | + |
| 14 | + "github.com/crossplane/upjet/pkg/registry" |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/scaleway/crossplane-provider-scaleway/config/tools" |
| 17 | + |
| 18 | + "gopkg.in/yaml.v3" |
| 19 | +) |
| 20 | + |
| 21 | +// ProviderMetadata represents the structure of provider-metadata.yaml |
| 22 | +type ProviderMetadata struct { |
| 23 | + Resources map[string]*registry.Resource `yaml:"resources"` |
| 24 | +} |
| 25 | + |
| 26 | +func main() { |
| 27 | + currentProviderMetadata := os.Getenv("CURRENT_METADATA") |
| 28 | + newProviderMetadata := os.Getenv("NEW_METADATA") |
| 29 | + |
| 30 | + currentResources, err := parseProviderMetadata(currentProviderMetadata) |
| 31 | + if err != nil { |
| 32 | + fmt.Printf("Error parsing current provider metadata: %v\n", err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + newResources, err := parseProviderMetadata(newProviderMetadata) |
| 37 | + if err != nil { |
| 38 | + fmt.Printf("Error parsing new provider metadata: %v\n", err) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + addedResources := findNewResources(currentResources, newResources) |
| 43 | + fmt.Println("New resources found:") |
| 44 | + resourceConfigs := make([]tools.ResourceConfig, 0, len(addedResources)) |
| 45 | + |
| 46 | + for _, resource := range addedResources { |
| 47 | + references := make(map[string]string) |
| 48 | + |
| 49 | + for _, example := range resource.Examples { |
| 50 | + for refKey, refValue := range example.References { |
| 51 | + resourceType := strings.Split(refValue, ".")[0] |
| 52 | + references[refKey] = resourceType |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + raw := generatePackageName(resource.SubCategory) |
| 57 | + |
| 58 | + aliases := map[string]string{ |
| 59 | + "transactionalemail": "tem", |
| 60 | + "domainsanddns": "domain", |
| 61 | + } |
| 62 | + |
| 63 | + pkg, ok := aliases[raw] |
| 64 | + if !ok { |
| 65 | + pkg = raw |
| 66 | + } |
| 67 | + |
| 68 | + config := tools.ResourceConfig{ |
| 69 | + PackageName: pkg, |
| 70 | + ShortGroup: pkg, |
| 71 | + ResourceName: resource.Title, |
| 72 | + TerraformResourceName: resource.Name, |
| 73 | + Kind: parseKindFromResourceName(resource.Name), |
| 74 | + References: references, |
| 75 | + } |
| 76 | + resourceConfigs = append(resourceConfigs, config) |
| 77 | + fmt.Println(resource.Name) |
| 78 | + } |
| 79 | + |
| 80 | + jsonData, err := json.Marshal(resourceConfigs) |
| 81 | + if err != nil { |
| 82 | + fmt.Printf("Error marshaling resource configuration: %v\n", err) |
| 83 | + return |
| 84 | + } |
| 85 | + fmt.Println(string(jsonData)) |
| 86 | +} |
| 87 | + |
| 88 | +func parseProviderMetadata(metadata string) (map[string]*registry.Resource, error) { |
| 89 | + var providerMetadata ProviderMetadata |
| 90 | + err := yaml.Unmarshal([]byte(metadata), &providerMetadata) |
| 91 | + if err != nil { |
| 92 | + return nil, errors.Wrap(err, "Failed to unmarshal provider metadata") |
| 93 | + } |
| 94 | + return providerMetadata.Resources, nil |
| 95 | +} |
| 96 | + |
| 97 | +func findNewResources(current, new map[string]*registry.Resource) []*registry.Resource { |
| 98 | + var addedResources []*registry.Resource |
| 99 | + for resourceName, resource := range new { |
| 100 | + if _, exists := current[resourceName]; !exists { |
| 101 | + addedResources = append(addedResources, resource) |
| 102 | + } |
| 103 | + } |
| 104 | + return addedResources |
| 105 | +} |
| 106 | + |
| 107 | +func parseKindFromResourceName(resourceName string) string { |
| 108 | + titleCaser := cases.Title(language.English) |
| 109 | + |
| 110 | + parts := strings.Split(resourceName, "_") |
| 111 | + if len(parts) == 0 { |
| 112 | + return "" |
| 113 | + } |
| 114 | + lastWord := parts[len(parts)-1] |
| 115 | + |
| 116 | + return titleCaser.String(lastWord) |
| 117 | +} |
| 118 | + |
| 119 | +func generatePackageName(subCategory string) string { |
| 120 | + re := regexp.MustCompile(`[^A-Za-z0-9]+`) |
| 121 | + cleaned := re.ReplaceAllString(subCategory, "") |
| 122 | + return strings.ToLower(cleaned) |
| 123 | +} |
0 commit comments