Skip to content

Commit

Permalink
fix: refactor form post functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stebenz committed Dec 22, 2022
1 parent 78f4565 commit cfbda7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
24 changes: 8 additions & 16 deletions zitadel/v2/helper/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,21 @@ func createMultipartRequest(issuer, endpoint, path string) (*http.Request, error
}

func InstanceFormFilePost(clientInfo *ClientInfo, endpoint, path string) diag.Diagnostics {
r, err := createMultipartRequest(clientInfo.Issuer, endpoint, path)
if err != nil {
return diag.Errorf("failed to create asset request: %v", err)
}

client, err := NewClientWithInterceptor(clientInfo.Issuer, clientInfo.KeyPath, []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()})
if err != nil {
return diag.Errorf("failed to create client: %v", err)
}
return formFilePost(clientInfo, endpoint, path, map[string]string{})
}

resp, err := client.Do(r)
if err != nil || resp.StatusCode != http.StatusOK {
return diag.Errorf("failed to do asset request: %v", err)
}
return nil
func OrgFormFilePost(clientInfo *ClientInfo, endpoint, path, orgID string) diag.Diagnostics {
return formFilePost(clientInfo, endpoint, path, map[string]string{"x-zitadel-orgid": orgID})
}

func OrgFormFilePost(clientInfo *ClientInfo, endpoint, orgID, path string) diag.Diagnostics {
func formFilePost(clientInfo *ClientInfo, endpoint, path string, additionalHeaders map[string]string) diag.Diagnostics {
r, err := createMultipartRequest(clientInfo.Issuer, endpoint, path)
if err != nil {
return diag.Errorf("failed to create asset request: %v", err)
}
r.Header.Add("x-zitadel-orgid", orgID)
for k, v := range additionalHeaders {
r.Header.Add(k, v)
}

client, err := NewClientWithInterceptor(clientInfo.Issuer, clientInfo.KeyPath, []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()})
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions zitadel/v2/label_policy/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,27 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia
}

if d.HasChanges(logoHashVar, logoPathVar) {
if err := helper.OrgFormFilePost(clientinfo, logoURL, org, d.Get(logoPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, logoURL, d.Get(logoPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload logo: %v", err)
}
}
if d.HasChanges(logoDarkHashVar, logoDarkPathVar) {
if err := helper.OrgFormFilePost(clientinfo, logoDarkURL, org, d.Get(logoDarkPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, logoDarkURL, d.Get(logoDarkPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload logo dark: %v", err)
}
}
if d.HasChanges(iconHashVar, iconPathVar) {
if err := helper.OrgFormFilePost(clientinfo, iconURL, org, d.Get(iconPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, iconURL, d.Get(iconPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload icon: %v", err)
}
}
if d.HasChanges(iconDarkHashVar, iconDarkPathVar) {
if err := helper.OrgFormFilePost(clientinfo, iconDarkURL, org, d.Get(iconDarkPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, iconDarkURL, d.Get(iconDarkPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload icon dark: %v", err)
}
}
if d.HasChanges(fontHashVar, fontPathVar) {
if err := helper.OrgFormFilePost(clientinfo, fontURL, org, d.Get(fontPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, fontURL, d.Get(fontPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload font: %v", err)
}
}
Expand Down Expand Up @@ -160,27 +160,27 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia
d.SetId(org)

if d.Get(logoHashVar) != "" && d.Get(logoPathVar) != "" {
if err := helper.OrgFormFilePost(clientinfo, logoURL, org, d.Get(logoPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, logoURL, d.Get(logoPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload logo: %v", err)
}
}
if d.Get(logoDarkHashVar) != "" && d.Get(logoDarkPathVar) != "" {
if err := helper.OrgFormFilePost(clientinfo, logoDarkURL, org, d.Get(logoDarkPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, logoDarkURL, d.Get(logoDarkPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload logo dark: %v", err)
}
}
if d.Get(iconHashVar) != "" && d.Get(iconPathVar) != "" {
if err := helper.OrgFormFilePost(clientinfo, iconURL, org, d.Get(iconPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, iconURL, d.Get(iconPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload icon: %v", err)
}
}
if d.Get(iconDarkHashVar) != "" && d.Get(iconDarkPathVar) != "" {
if err := helper.OrgFormFilePost(clientinfo, iconDarkURL, org, d.Get(iconDarkPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, iconDarkURL, d.Get(iconDarkPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload icon dark: %v", err)
}
}
if d.Get(fontHashVar) != "" && d.Get(fontPathVar) != "" {
if err := helper.OrgFormFilePost(clientinfo, fontURL, org, d.Get(fontPathVar).(string)); err != nil {
if err := helper.OrgFormFilePost(clientinfo, fontURL, d.Get(fontPathVar).(string), org); err != nil {
return diag.Errorf("failed to upload font: %v", err)
}
}
Expand Down

0 comments on commit cfbda7a

Please sign in to comment.