Skip to content

Commit

Permalink
chore: separate count for ns, crd and deploy yaml files
Browse files Browse the repository at this point in the history
Added logic to separate the ns, crs and deploy yaml file with different prefix
for each of the following set.
Also exposed new state vars to store the count of such files
  • Loading branch information
atulatnirmata committed Jul 1, 2023
1 parent 28f21ec commit f27dd81
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions pkg/nirmata/resource_cluster_registered.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ var registeredClusterSchema = map[string]*schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"controller_ns_yamls_count": {
Type: schema.TypeInt,
Computed: true,
},
"controller_crd_yamls_count": {
Type: schema.TypeInt,
Computed: true,
},
"controller_deploy_yamls_count": {
Type: schema.TypeInt,
Computed: true,
},
"delete_action": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -372,14 +384,17 @@ func resourceClusterRegisteredCreate(d *schema.ResourceData, meta interface{}) e
d.Set("controller_yamls", yaml)
d.Set("state", clusterObj["state"])

path, count, ferr := writeToTempDir(controller_yamls_folder, []byte(yaml))
path, count, count_ns, count_crd, count_deploy, ferr := writeToTempDir(controller_yamls_folder, []byte(yaml))
if ferr != nil {
return fmt.Errorf("failed to write temporary files: %v", ferr)
}

log.Printf("[INFO] - wrote temporary YAMLs files to %s:", path)

d.Set("controller_yamls_count", count)
d.Set("controller_ns_yamls_count", count_ns)
d.Set("controller_crd_yamls_count", count_crd)
d.Set("controller_deploy_yamls_count", count_deploy)
d.Set("controller_yamls_folder", path)
return nil
}
Expand Down Expand Up @@ -411,14 +426,14 @@ func IsDirEmpty(name string) (bool, error) {
return false, err
}

func writeToTempDir(controller_yamls_folder string, data []byte) (path string, count int, err error) {
func writeToTempDir(controller_yamls_folder string, data []byte) (path string, count, count_ns, count_crd, count_deploy int, err error) {
if controller_yamls_folder != "" {
if _, err := os.Stat(controller_yamls_folder); os.IsNotExist(err) {
return "", 0, fmt.Errorf("Folder '%s' does not exist", controller_yamls_folder)
return "", 0, 0, 0, 0, fmt.Errorf("Folder '%s' does not exist", controller_yamls_folder)
}
empty, _ := IsDirEmpty(controller_yamls_folder)
if !empty {
return "", 0, fmt.Errorf("Folder '%s' is not empty", controller_yamls_folder)
return "", 0, 0, 0, 0, fmt.Errorf("Folder '%s' is not empty", controller_yamls_folder)
}
path = controller_yamls_folder
} else {
Expand All @@ -430,18 +445,34 @@ func writeToTempDir(controller_yamls_folder string, data []byte) (path string, c

result := strings.Split(string(data), "---")
count = 0
count_ns = 0
count_crd = 0
count_deploy = 0
for _, r := range result {
if r == "" {
continue
}

f, err := ioutil.TempFile(path, "temp-")
fileContents := fmt.Sprintf("%s", r)
prefix := "temp%s"
log.Printf("[DEBUG] fileContents %s", fileContents)
if (strings.Contains(fileContents, "kind: Namespace") || strings.Contains(fileContents, "kind: \"Namespace\"")) {
prefix = fmt.Sprintf(prefix, "-01-")
count_ns += 1
} else if (strings.Contains(fileContents, "kind: Deployment") || strings.Contains(fileContents, "kind: \"Deployment\"")) {
prefix = fmt.Sprintf(prefix, "-03-")
count_deploy += 1
} else {
prefix = fmt.Sprintf(prefix, "-02-")
count_crd += 1
}
f, err := ioutil.TempFile(path, prefix)
if err != nil {
return "", 0, fmt.Errorf("cannot create temporary file: %v", err)
return "", 0, 0, 0, 0, fmt.Errorf("cannot create temporary file: %v", err)
}

if _, err = f.Write([]byte(r)); err != nil {
return "", 0, fmt.Errorf("failed to write temporary file %s: %v", f.Name(), err)
return "", 0, 0, 0, 0, fmt.Errorf("failed to write temporary file %s: %v", f.Name(), err)
}

count += 1
Expand Down

0 comments on commit f27dd81

Please # to comment.