Skip to content

Commit

Permalink
Cleanup internal folder
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Sep 3, 2024
1 parent 8b2fb56 commit b30c879
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 272 deletions.
36 changes: 16 additions & 20 deletions internal/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ func ReadActiveApplications() (apiTypes.GitopsCatalogApps, error) {

activeContent, err := gh.ReadGitopsCatalogRepoContents()
if err != nil {
return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog repository content: %s", err)
return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog repository content: %w", err)
}

index, err := gh.ReadGitopsCatalogIndex(activeContent)
if err != nil {
return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog index content: %s", err)
return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog index content: %w", err)
}

var out apiTypes.GitopsCatalogApps

err = yaml.Unmarshal(index, &out)
if err != nil {
return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog applications: %s", err)
return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog applications: %w", err)
}

return out, nil
Expand All @@ -71,7 +71,7 @@ func ValidateCatalogApps(catalogApps string) (bool, []apiTypes.GitopsCatalogApp,

apps, err := ReadActiveApplications()
if err != nil {
log.Error().Msgf(fmt.Sprintf("Error getting gitops catalag applications: %s", err))
log.Error().Msgf("error getting gitops catalog applications: %s", err)
return false, gitopsCatalogapps, err
}

Expand All @@ -86,7 +86,7 @@ func ValidateCatalogApps(catalogApps string) (bool, []apiTypes.GitopsCatalogApp,
secretValue := os.Getenv(secret.Env)

if secretValue == "" {
return false, gitopsCatalogapps, fmt.Errorf("your %s environment variable is not set for %s catalog application. Please set and try again", secret.Env, app)
return false, gitopsCatalogapps, fmt.Errorf("your %q environment variable is not set for %q catalog application. Please set and try again", secret.Env, app)
}

secret.Value = secretValue
Expand All @@ -97,7 +97,7 @@ func ValidateCatalogApps(catalogApps string) (bool, []apiTypes.GitopsCatalogApp,
for _, config := range catalogApp.ConfigKeys {
configValue := os.Getenv(config.Env)
if configValue == "" {
return false, gitopsCatalogapps, fmt.Errorf("your %s environment variable is not set for %s catalog application. Please set and try again", config.Env, app)
return false, gitopsCatalogapps, fmt.Errorf("your %q environment variable is not set for %q catalog application. Please set and try again", config.Env, app)
}
config.Value = configValue
}
Expand All @@ -109,7 +109,7 @@ func ValidateCatalogApps(catalogApps string) (bool, []apiTypes.GitopsCatalogApp,
}
}
if !found {
return false, gitopsCatalogapps, fmt.Errorf(fmt.Sprintf("catalag app is not supported: %s", app))
return false, gitopsCatalogapps, fmt.Errorf("catalog app is not supported: %q", app)
}
}

Expand All @@ -125,7 +125,7 @@ func (gh *GitHubClient) ReadGitopsCatalogRepoContents() ([]*git.RepositoryConten
nil,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("error retrieving gitops catalog repository contents: %w", err)
}

return directoryContent, nil
Expand All @@ -134,20 +134,16 @@ func (gh *GitHubClient) ReadGitopsCatalogRepoContents() ([]*git.RepositoryConten
// ReadGitopsCatalogIndex reads the gitops catalog repository index
func (gh *GitHubClient) ReadGitopsCatalogIndex(contents []*git.RepositoryContent) ([]byte, error) {
for _, content := range contents {
switch *content.Type {
case "file":
switch *content.Name {
case "index.yaml":
b, err := gh.readFileContents(content)
if err != nil {
return b, err
}
return b, nil
if *content.Type == "file" && *content.Name == "index.yaml" {
b, err := gh.readFileContents(content)
if err != nil {
return nil, fmt.Errorf("error reading index.yaml file: %w", err)
}
return b, nil
}
}

return []byte{}, fmt.Errorf("index.yaml not found in gitops catalog repository")
return nil, fmt.Errorf("index.yaml not found in gitops catalog repository")
}

// readFileContents parses the contents of a file in a GitHub repository
Expand All @@ -160,13 +156,13 @@ func (gh *GitHubClient) readFileContents(content *git.RepositoryContent) ([]byte
nil,
)
if err != nil {
return []byte{}, err
return nil, fmt.Errorf("error downloading contents of %q: %w", *content.Path, err)
}
defer rc.Close()

b, err := io.ReadAll(rc)
if err != nil {
return []byte{}, err
return nil, fmt.Errorf("error reading contents of %q: %w", *content.Path, err)
}

return b, nil
Expand Down
98 changes: 48 additions & 50 deletions internal/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,36 @@ func CreateCluster(cluster apiTypes.ClusterDefinition) error {

payload, err := json.Marshal(requestObject)
if err != nil {
return err
return fmt.Errorf("failed to marshal request object: %w", err)
}

req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/proxy", GetConsoleIngresUrl()), bytes.NewReader(payload))
if err != nil {
log.Info().Msgf("error %s", err)
return err
log.Printf("error creating request: %s", err)
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

res, err := httpClient.Do(req)
if err != nil {
log.Info().Msgf("error %s", err)
return err
log.Printf("error executing request: %s", err)
return fmt.Errorf("failed to execute request: %w", err)
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
log.Info().Msgf("unable to create cluster %s", err)

return err
log.Printf("unable to create cluster: %v", err)
return fmt.Errorf("failed to read response body: %w", err)
}

if res.StatusCode != http.StatusAccepted {
log.Info().Msgf("unable to create cluster %s %s", res.Status, body)
return fmt.Errorf("unable to create cluster %s %s", res.Status, body)
log.Printf("unable to create cluster: %q %q", res.Status, body)
return fmt.Errorf("unable to create cluster: API returned unexpected status code %q: %s", res.Status, body)
}

log.Info().Msgf("Created cluster: %s", string(body))
log.Printf("Created cluster: %q", string(body))

return nil
}
Expand All @@ -85,38 +84,36 @@ func ResetClusterProgress(clusterName string) error {

payload, err := json.Marshal(requestObject)
if err != nil {
return err
return fmt.Errorf("failed to marshal request object: %w", err)
}

req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/proxy", GetConsoleIngresUrl()), bytes.NewReader(payload))
if err != nil {
log.Info().Msgf("error %s", err)
return err
log.Printf("error creating request: %v", err)
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

res, err := httpClient.Do(req)
if err != nil {
log.Info().Msgf("error %s", err)
return err
log.Printf("error executing request: %v", err)
return fmt.Errorf("failed to execute request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
log.Info().Msgf("unable to create cluster %s", res.Status)
return err
log.Printf("unable to reset cluster progress: %q", res.Status)
return fmt.Errorf("unable to reset cluster progress: API returned unexpected status %q", res.Status)
}

body, err := io.ReadAll(res.Body)
if err != nil {
log.Info().Msgf("unable to create cluster %s", err)

return err
log.Printf("unable to read response body: %v", err)
return fmt.Errorf("failed to read response body: %w", err)
}

log.Info().Msgf("Import: %s", string(body))

return nil
}

Expand All @@ -127,34 +124,34 @@ func GetCluster(clusterName string) (apiTypes.Cluster, error) {
cluster := apiTypes.Cluster{}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/proxy?url=/cluster/%s", GetConsoleIngresUrl(), clusterName), nil)
if err != nil {
log.Info().Msgf("error %s", err)
return cluster, err
log.Printf("error creating request: %v", err)
return cluster, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

res, err := httpClient.Do(req)
if err != nil {
log.Info().Msgf("error %s", err)
return cluster, err
log.Printf("error executing request: %v", err)
return cluster, fmt.Errorf("failed to execute request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
log.Info().Msgf("unable to get cluster %s, continuing", res.Status)
return cluster, err
log.Printf("unable to get cluster: %q, continuing", res.Status)
return cluster, fmt.Errorf("unable to get cluster: %q", res.Status)
}

body, err := io.ReadAll(res.Body)
if err != nil {
log.Info().Msgf("unable to get cluster %s", err)
return cluster, err
log.Printf("unable to read response body: %v", err)
return cluster, fmt.Errorf("failed to read response body: %w", err)
}

err = json.Unmarshal(body, &cluster)
if err != nil {
log.Info().Msgf("unable to cast cluster object %s", err)
return cluster, err
log.Printf("unable to unmarshal cluster object: %v", err)
return cluster, fmt.Errorf("failed to unmarshal cluster object: %w", err)
}

return cluster, nil
Expand All @@ -167,33 +164,34 @@ func GetClusters() ([]apiTypes.Cluster, error) {
clusters := []apiTypes.Cluster{}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/proxy?url=/cluster", GetConsoleIngresUrl()), nil)
if err != nil {
log.Info().Msgf("error %s", err)
return clusters, err
log.Printf("error creating request: %v", err)
return clusters, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

res, err := httpClient.Do(req)
if err != nil {
log.Info().Msgf("error %s", err)
return clusters, err
log.Printf("error executing request: %v", err)
return clusters, fmt.Errorf("failed to execute request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
log.Info().Msgf("unable to get clusters %s, continuing", res.Status)
return clusters, err
log.Printf("unable to get clusters: %q", res.Status)
return clusters, fmt.Errorf("unable to get clusters: API returned unexpected status code %q", res.Status)
}

body, err := io.ReadAll(res.Body)
if err != nil {
log.Info().Msgf("unable to get clusters %s", err)
return clusters, err
log.Printf("unable to read response body: %v", err)
return clusters, fmt.Errorf("failed to read response body: %w", err)
}

err = json.Unmarshal(body, &clusters)
if err != nil {
log.Info().Msgf("unable to cast clusters object %s", err)
return clusters, err
log.Printf("unable to unmarshal clusters object: %v", err)
return clusters, fmt.Errorf("failed to unmarshal clusters object: %w", err)
}

return clusters, nil
Expand All @@ -205,27 +203,27 @@ func DeleteCluster(clusterName string) error {

req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/proxy?url=/cluster/%s", GetConsoleIngresUrl(), clusterName), nil)
if err != nil {
log.Info().Msgf("error %s", err)
return err
log.Printf("error creating request: %v", err)
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

res, err := httpClient.Do(req)
if err != nil {
log.Info().Msgf("error %s", err)
return err
log.Printf("error executing request: %v", err)
return fmt.Errorf("failed to execute request: %w", err)
}

if res.StatusCode != http.StatusOK {
log.Info().Msgf("unable to delete cluster %s, continuing", res.Status)
return err
log.Printf("unable to delete cluster: %q, continuing", res.Status)
return fmt.Errorf("unable to delete cluster: API returned unexpected status code %q", res.Status)
}

_, err = io.ReadAll(res.Body)
if err != nil {
log.Info().Msgf("unable to delete cluster %s", err)
return err
log.Printf("unable to read response body: %v", err)
return fmt.Errorf("failed to read response body: %w", err)
}

return nil
Expand Down
Loading

0 comments on commit b30c879

Please sign in to comment.