|
| 1 | +// describer.go |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/opengovern/og-describer-github/describer/pkg/describer" |
| 15 | + model "github.com/opengovern/og-describer-github/describer/pkg/sdk/models" |
| 16 | + "github.com/opengovern/og-describer-github/describer/pkg/wrapper" |
| 17 | + "github.com/opengovern/og-describer-github/describer/provider" |
| 18 | + "github.com/opengovern/og-describer-github/describer/provider/configs" |
| 19 | + "github.com/opengovern/og-util/pkg/describe" |
| 20 | + "github.com/opengovern/og-util/pkg/es" |
| 21 | + "github.com/spf13/cobra" |
| 22 | + "go.uber.org/zap" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + resourceType string |
| 27 | + outputFile string |
| 28 | +) |
| 29 | + |
| 30 | +// describerCmd represents the describer command |
| 31 | +var describerCmd = &cobra.Command{ |
| 32 | + Use: "describer", |
| 33 | + Short: "A brief description of your command", |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + // Environment takes priority |
| 36 | + orgEnv := os.Getenv("GITHUB_ORG") |
| 37 | + patEnv := os.Getenv("GITHUB_PAT") |
| 38 | + |
| 39 | + if orgEnv != "" { |
| 40 | + OrganizationName = orgEnv |
| 41 | + } |
| 42 | + |
| 43 | + if patEnv != "" { |
| 44 | + PatToken = patEnv |
| 45 | + } |
| 46 | + |
| 47 | + // Open the output file |
| 48 | + file, err := os.Create(outputFile) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to create file: %w", err) |
| 51 | + } |
| 52 | + defer file.Close() // Ensure the file is closed at the end |
| 53 | + |
| 54 | + job := describe.DescribeJob{ |
| 55 | + JobID: uint(uuid.New().ID()), |
| 56 | + ResourceType: resourceType, |
| 57 | + IntegrationID: "", |
| 58 | + ProviderID: "", |
| 59 | + DescribedAt: time.Now().UnixMilli(), |
| 60 | + IntegrationType: configs.IntegrationTypeLower, |
| 61 | + CipherText: "", |
| 62 | + IntegrationLabels: map[string]string{ |
| 63 | + "OrganizationName": OrganizationName, |
| 64 | + }, |
| 65 | + IntegrationAnnotations: nil, |
| 66 | + } |
| 67 | + |
| 68 | + ctx := context.Background() |
| 69 | + logger, _ := zap.NewProduction() |
| 70 | + |
| 71 | + creds, err := provider.AccountCredentialsFromMap(map[string]any{ |
| 72 | + "pat_token": PatToken, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf(" account credentials: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + additionalParameters, err := provider.GetAdditionalParameters(job) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + plg := wrapper.Plugin() |
| 83 | + |
| 84 | + f := func(resource model.Resource) error { |
| 85 | + if resource.Description == nil { |
| 86 | + return nil |
| 87 | + } |
| 88 | + descriptionJSON, err := json.Marshal(resource.Description) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to marshal description: %w", err) |
| 91 | + } |
| 92 | + descriptionJSON, err = trimJsonFromEmptyObjects(descriptionJSON) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to trim json: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + metadata, err := provider.GetResourceMetadata(job, resource) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to get resource metadata") |
| 100 | + } |
| 101 | + err = provider.AdjustResource(job, &resource) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("failed to adjust resource metadata") |
| 104 | + } |
| 105 | + |
| 106 | + desc := resource.Description |
| 107 | + err = json.Unmarshal(descriptionJSON, &desc) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("unmarshal description: %v", err.Error()) |
| 110 | + } |
| 111 | + |
| 112 | + if plg != nil { |
| 113 | + _, _, err = wrapper.ExtractTagsAndNames(logger, plg, job.ResourceType, resource) |
| 114 | + if err != nil { |
| 115 | + logger.Error("failed to build tags for service", zap.Error(err), zap.String("resourceType", job.ResourceType), zap.Any("resource", resource)) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + var description any |
| 120 | + err = json.Unmarshal([]byte(descriptionJSON), &description) |
| 121 | + if err != nil { |
| 122 | + logger.Error("failed to parse resource description json", zap.Error(err)) |
| 123 | + return fmt.Errorf("failed to parse resource description json") |
| 124 | + } |
| 125 | + |
| 126 | + res := es.Resource{ |
| 127 | + PlatformID: fmt.Sprintf("%s:::%s:::%s", job.IntegrationID, job.ResourceType, resource.UniqueID()), |
| 128 | + ResourceID: resource.UniqueID(), |
| 129 | + ResourceName: resource.Name, |
| 130 | + Description: description, |
| 131 | + IntegrationType: configs.IntegrationName, |
| 132 | + ResourceType: strings.ToLower(job.ResourceType), |
| 133 | + IntegrationID: job.IntegrationID, |
| 134 | + Metadata: metadata, |
| 135 | + DescribedAt: job.DescribedAt, |
| 136 | + DescribedBy: strconv.FormatUint(uint64(job.JobID), 10), |
| 137 | + } |
| 138 | + |
| 139 | + // Write the resource JSON to the file |
| 140 | + resJSON, err := json.Marshal(res) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("failed to marshal resource JSON: %w", err) |
| 143 | + } |
| 144 | + _, err = file.Write(resJSON) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("failed to write to file: %w", err) |
| 147 | + } |
| 148 | + _, err = file.Write([]byte(",\n")) // Add a newline for readability |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("failed to write newline to file: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | + } |
| 155 | + clientStream := (*model.StreamSender)(&f) |
| 156 | + |
| 157 | + err = describer.GetResources( |
| 158 | + ctx, |
| 159 | + logger, |
| 160 | + job.ResourceType, |
| 161 | + job.TriggerType, |
| 162 | + creds, |
| 163 | + additionalParameters, |
| 164 | + clientStream, |
| 165 | + ) |
| 166 | + if err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + return nil |
| 170 | + }, |
| 171 | +} |
| 172 | + |
| 173 | +func init() { |
| 174 | + describerCmd.Flags().StringVar(&resourceType, "resourceType", "", "Resource type") |
| 175 | + describerCmd.Flags().StringVar(&outputFile, "outputFile", "output.json", "File to write JSON outputs") |
| 176 | +} |
| 177 | + |
| 178 | +func trimJsonFromEmptyObjects(input []byte) ([]byte, error) { |
| 179 | + unknownData := map[string]any{} |
| 180 | + err := json.Unmarshal(input, &unknownData) |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + trimEmptyMaps(unknownData) |
| 185 | + return json.Marshal(unknownData) |
| 186 | +} |
| 187 | + |
| 188 | +func trimEmptyMaps(input map[string]any) { |
| 189 | + for key, value := range input { |
| 190 | + switch value.(type) { |
| 191 | + case map[string]any: |
| 192 | + if len(value.(map[string]any)) != 0 { |
| 193 | + trimEmptyMaps(value.(map[string]any)) |
| 194 | + } |
| 195 | + if len(value.(map[string]any)) == 0 { |
| 196 | + delete(input, key) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments