File: keyAuth.go

package info (click to toggle)
deck 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: makefile: 17; sh: 3
file content (94 lines) | stat: -rw-r--r-- 2,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package diff

import (
	"github.com/kong/deck/crud"
	"github.com/kong/deck/state"
	"github.com/pkg/errors"
)

func (sc *Syncer) deleteKeyAuths() error {
	currentKeyAuths, err := sc.currentState.KeyAuths.GetAll()
	if err != nil {
		return errors.Wrap(err, "error fetching key-auths from state")
	}

	for _, keyAuth := range currentKeyAuths {
		n, err := sc.deleteKeyAuth(keyAuth)
		if err != nil {
			return err
		}
		if n != nil {
			err = sc.queueEvent(*n)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (sc *Syncer) deleteKeyAuth(keyAuth *state.KeyAuth) (*Event, error) {
	_, err := sc.targetState.KeyAuths.Get(*keyAuth.ID)
	if err == state.ErrNotFound {
		return &Event{
			Op:   crud.Delete,
			Kind: "key-auth",
			Obj:  keyAuth,
		}, nil
	}
	if err != nil {
		return nil, errors.Wrapf(err, "looking up key-auth '%v'", *keyAuth.ID)
	}
	return nil, nil
}

func (sc *Syncer) createUpdateKeyAuths() error {
	targetKeyAuths, err := sc.targetState.KeyAuths.GetAll()
	if err != nil {
		return errors.Wrap(err, "error fetching key-auths from state")
	}

	for _, keyAuth := range targetKeyAuths {
		n, err := sc.createUpdateKeyAuth(keyAuth)
		if err != nil {
			return err
		}
		if n != nil {
			err = sc.queueEvent(*n)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (sc *Syncer) createUpdateKeyAuth(keyAuth *state.KeyAuth) (*Event, error) {
	keyAuth = &state.KeyAuth{KeyAuth: *keyAuth.DeepCopy()}
	currentKeyAuth, err := sc.currentState.KeyAuths.Get(*keyAuth.ID)
	if err == state.ErrNotFound {
		// keyAuth not present, create it

		return &Event{
			Op:   crud.Create,
			Kind: "key-auth",
			Obj:  keyAuth,
		}, nil
	}
	if err != nil {
		return nil, errors.Wrapf(err, "error looking up key-auth %v",
			*keyAuth.ID)
	}
	// found, check if update needed

	if !currentKeyAuth.EqualWithOpts(keyAuth, false, true, false) {

		return &Event{
			Op:     crud.Update,
			Kind:   "key-auth",
			Obj:    keyAuth,
			OldObj: currentKeyAuth,
		}, nil
	}
	return nil, nil
}