Skip to content

Commit 21ad550

Browse files
authored
Merge pull request #20 from opengovern/fix-folder-reorganization
fix: fix makefile
2 parents 1ccf998 + d0ee1e8 commit 21ad550

File tree

8 files changed

+416
-4
lines changed

8 files changed

+416
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ build/
22
og-describer-template
33
.idea
44
.env
5-
local/
5+
.vscode

describer/local/LICENSE

Whitespace-only changes.

describer/local/cmd/describer.go

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

0 commit comments

Comments
 (0)