Skip to content

Commit

Permalink
fix: fixed all describers
Browse files Browse the repository at this point in the history
  • Loading branch information
ArshiaBP committed Dec 11, 2024
1 parent 81acd10 commit 6447db0
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 34 deletions.
4 changes: 3 additions & 1 deletion command/cmd/describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ var describerCmd = &cobra.Command{
logger, _ := zap.NewProduction()

// TODO: Set the credentials
creds := configs.IntegrationCredentials{}
creds := configs.IntegrationCredentials{
APIKey: "rnd_Uw1LgzMfbL1mPqbLNbkVjfnZDaWx",
}

additionalParameters, err := provider.GetAdditionalParameters(job)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"es_id":"","es_index":"","platform_id":":::Render/EnvGroup:::evg-ctavhehopnds73ejl14g","resource_id":"evg-ctavhehopnds73ejl14g","resource_name":"harbor-prod","description":{"createdAt":"2024-12-08T19:51:22.426806Z","environmentId":"","id":"evg-ctavhehopnds73ejl14g","name":"harbor-prod","ownerId":"tea-csuhvt52ng1s739lbn5g","serviceLinks":[],"updatedAt":"2024-12-08T19:51:22.426806Z"},"integration_type":"RENDER_ACCOUNT","resource_type":"render/envgroup","integration_id":"","metadata":{},"canonical_tags":null,"described_by":"473050548","described_at":1733908008835},
3 changes: 1 addition & 2 deletions provider/describer/deploys.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func processDeploys(ctx context.Context, handler *RenderAPIHandler, serviceID st
if cursor != "" {
params.Set("cursor", cursor)
}
finalURL := fmt.Sprintf("%s%sdeploys?%s", baseURL, serviceID, params.Encode())

finalURL := fmt.Sprintf("%s%s/deploys?%s", baseURL, serviceID, params.Encode())
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
Expand Down
58 changes: 38 additions & 20 deletions provider/describer/env_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,51 @@ func GetEnvGroup(ctx context.Context, handler *RenderAPIHandler, resourceID stri

func processEnvGroups(ctx context.Context, handler *RenderAPIHandler, renderChan chan<- models.Resource, wg *sync.WaitGroup) error {
var envGroups []model.EnvGroupDescription
var envGroupResp []model.EnvGroupResponse
var resp *http.Response
baseURL := "https://api.render.com/v1/env-groups"
params := url.Values{}
params.Set("limit", limit)
finalURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
cursor := ""

req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
for {
params := url.Values{}
params.Set("limit", limit)
if cursor != "" {
params.Set("cursor", cursor)
}
finalURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())

requestFunc := func(req *http.Request) (*http.Response, error) {
var e error
resp, e = handler.Client.Do(req)
if e != nil {
return nil, fmt.Errorf("request execution failed: %w", e)
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
defer resp.Body.Close()

if e = json.NewDecoder(resp.Body).Decode(&envGroups); e != nil {
return nil, fmt.Errorf("failed to decode response: %w", e)
requestFunc := func(req *http.Request) (*http.Response, error) {
var e error
resp, e = handler.Client.Do(req)
if e != nil {
return nil, fmt.Errorf("request execution failed: %w", e)
}
defer resp.Body.Close()

if e = json.NewDecoder(resp.Body).Decode(&envGroupResp); e != nil {
return nil, fmt.Errorf("failed to decode response: %w", e)
}
for i, envGroup := range envGroupResp {
envGroups = append(envGroups, envGroup.EnvGroup)
if i == len(envGroupResp)-1 {
cursor = envGroup.Cursor
}
}
return resp, nil
}
err = handler.DoRequest(ctx, req, requestFunc)
if err != nil {
return fmt.Errorf("error during request handling: %w", err)
}

if len(envGroupResp) < 100 {
break
}
return resp, nil
}
err = handler.DoRequest(ctx, req, requestFunc)
if err != nil {
return fmt.Errorf("error during request handling: %w", err)
}
for _, envGroup := range envGroups {
wg.Add(1)
Expand Down
13 changes: 10 additions & 3 deletions provider/describer/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ func ListEnvironments(ctx context.Context, handler *RenderAPIHandler, stream *mo
var wg sync.WaitGroup
renderChan := make(chan models.Resource)
errorChan := make(chan error, 1) // Buffered channel to capture errors
projects, err := getProjects(ctx, handler)
if err != nil {
return nil, err
}

go func() {
defer close(renderChan)
defer close(errorChan)
if err := processEnvironments(ctx, handler, renderChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
for _, project := range projects {
if err := processEnvironments(ctx, handler, project.ID, renderChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
}
}
wg.Wait()
}()
Expand Down Expand Up @@ -60,7 +66,7 @@ func GetEnvironment(ctx context.Context, handler *RenderAPIHandler, resourceID s
return &value, nil
}

func processEnvironments(ctx context.Context, handler *RenderAPIHandler, renderChan chan<- models.Resource, wg *sync.WaitGroup) error {
func processEnvironments(ctx context.Context, handler *RenderAPIHandler, projectID string, renderChan chan<- models.Resource, wg *sync.WaitGroup) error {
var environments []model.EnvironmentDescription
var environmentListResponse []model.EnvironmentResponse
var resp *http.Response
Expand All @@ -69,6 +75,7 @@ func processEnvironments(ctx context.Context, handler *RenderAPIHandler, renderC

for {
params := url.Values{}
params.Set("projectId", projectID)
params.Set("limit", limit)
if cursor != "" {
params.Set("cursor", cursor)
Expand Down
9 changes: 5 additions & 4 deletions provider/describer/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func ListHeaders(ctx context.Context, handler *RenderAPIHandler, stream *models.
defer close(renderChan)
defer close(errorChan)
for _, service := range services {
if err := processHeaders(ctx, handler, service.ID, renderChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
if service.Type == "static_site" {
if err := processHeaders(ctx, handler, service.ID, renderChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
}
}
}
wg.Wait()
Expand Down Expand Up @@ -64,8 +66,7 @@ func processHeaders(ctx context.Context, handler *RenderAPIHandler, serviceID st
if cursor != "" {
params.Set("cursor", cursor)
}
finalURL := fmt.Sprintf("%s%sheaders?%s", baseURL, serviceID, params.Encode())

finalURL := fmt.Sprintf("%s%s/headers?%s", baseURL, serviceID, params.Encode())
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion provider/describer/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func processJobs(ctx context.Context, handler *RenderAPIHandler, serviceID strin
if cursor != "" {
params.Set("cursor", cursor)
}
finalURL := fmt.Sprintf("%s%sjobs?%s", baseURL, serviceID, params.Encode())
finalURL := fmt.Sprintf("%s%s/jobs?%s", baseURL, serviceID, params.Encode())

req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions provider/describer/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func ListRoutes(ctx context.Context, handler *RenderAPIHandler, stream *models.S
defer close(renderChan)
defer close(errorChan)
for _, service := range services {
if err := processRoutes(ctx, handler, service.ID, renderChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
if service.Type == "static_site" {
if err := processRoutes(ctx, handler, service.ID, renderChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
}
}
}
wg.Wait()
Expand Down Expand Up @@ -64,7 +66,7 @@ func processRoutes(ctx context.Context, handler *RenderAPIHandler, serviceID str
if cursor != "" {
params.Set("cursor", cursor)
}
finalURL := fmt.Sprintf("%s%sroutes?%s", baseURL, serviceID, params.Encode())
finalURL := fmt.Sprintf("%s%s/routes?%s", baseURL, serviceID, params.Encode())

req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions provider/describer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,54 @@ func getServices(ctx context.Context, handler *RenderAPIHandler) ([]model.Servic
}
return services, nil
}

func getProjects(ctx context.Context, handler *RenderAPIHandler) ([]model.ProjectDescription, error) {
var projects []model.ProjectDescription
var projectListResponse []model.ProjectResponse
var resp *http.Response
baseURL := "https://api.render.com/v1/projects"
cursor := ""

for {
params := url.Values{}
params.Set("limit", limit)
if cursor != "" {
params.Set("cursor", cursor)
}
finalURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())

req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

requestFunc := func(req *http.Request) (*http.Response, error) {
var e error
resp, e = handler.Client.Do(req)
if e != nil {
return nil, fmt.Errorf("request execution failed: %w", e)
}
defer resp.Body.Close()

if e = json.NewDecoder(resp.Body).Decode(&projectListResponse); e != nil {
return nil, fmt.Errorf("failed to decode response: %w", e)
}
for i, projectResp := range projectListResponse {
projects = append(projects, projectResp.Project)
if i == len(projectListResponse)-1 {
cursor = projectResp.Cursor
}
}
return resp, nil
}
err = handler.DoRequest(ctx, req, requestFunc)
if err != nil {
return nil, fmt.Errorf("error during request handling: %w", err)
}

if len(projectListResponse) < 100 {
break
}
}
return projects, nil
}
5 changes: 5 additions & 0 deletions provider/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ type ServiceLink struct {
Type string `json:"type"`
}

type EnvGroupResponse struct {
EnvGroup EnvGroupDescription `json:"envGroup"`
Cursor string `json:"cursor"`
}

type EnvGroupDescription struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down

0 comments on commit 6447db0

Please sign in to comment.