Skip to content

Commit

Permalink
Ensure logo updated when source file changes (#514)
Browse files Browse the repository at this point in the history
* Store computed logo hash alongside path

* Minor refactoring

Co-authored-by: bogdanprodan-okta <bogdan.prodan@okta.com>
  • Loading branch information
gavinbunney and bogdanprodan-okta authored Jun 28, 2021
1 parent ace76ed commit e856505
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions okta/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package okta

import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -80,6 +85,13 @@ var baseAppSchema = map[string]*schema.Schema{
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == ""
},
StateFunc: func(val interface{}) string {
logoPath := val.(string)
if logoPath == "" {
return logoPath
}
return fmt.Sprintf("%s (%s)", logoPath, computeFileHash(logoPath))
},
},
"logo_url": {
Type: schema.TypeString,
Expand Down Expand Up @@ -599,3 +611,18 @@ func listAppUsersAndGroupsIDs(ctx context.Context, client *okta.Client, id strin
}
return
}

func computeFileHash(filename string) string {
file, err := os.Open(filename)
if err != nil {
return ""
}
defer func() {
_ = file.Close()
}()
h := sha256.New()
if _, err := io.Copy(h, file); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(h.Sum(nil))
}

0 comments on commit e856505

Please sign in to comment.