Skip to content

Commit

Permalink
update etag
Browse files Browse the repository at this point in the history
  • Loading branch information
linyguo committed Nov 27, 2024
1 parent f0c3829 commit 9e7725f
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func (m *ActivationsManager) GetState(ctx context.Context, name string, namespac
return model.ActivationState{}, err
}
var ret model.ActivationState
ret, err = getActivationState(entry.Body, entry.ETag)
ret, err = getActivationState(entry.Body)
if err != nil {
log.ErrorfCtx(ctx, "Failed to convert to activation state for %s in namespace %s: %v", name, namespace, err)
return model.ActivationState{}, err
}
return ret, nil
}

func getActivationState(body interface{}, etag string) (model.ActivationState, error) {
func getActivationState(body interface{}) (model.ActivationState, error) {
var activationState model.ActivationState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &activationState)
Expand All @@ -111,7 +111,6 @@ func getActivationState(body interface{}, etag string) (model.ActivationState, e
if activationState.Spec == nil {
activationState.Spec = &model.ActivationSpec{}
}
activationState.ObjectMeta.ETag = etag
if activationState.Status == nil {
activationState.Status = &model.ActivationStatus{}
}
Expand Down Expand Up @@ -148,12 +147,12 @@ func (m *ActivationsManager) UpsertState(ctx context.Context, name string, state
entry, err = m.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
itemState, err := getActivationState(entry.Body, entry.ETag)
itemState, err := getActivationState(entry.Body)
if err != nil {
log.ErrorfCtx(ctx, "Failed to convert to activation state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err)
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta)
}

if m.needValidate {
Expand Down Expand Up @@ -243,7 +242,7 @@ func (t *ActivationsManager) ListState(ctx context.Context, namespace string) ([
ret := make([]model.ActivationState, 0)
for _, t := range activations {
var rt model.ActivationState
rt, err = getActivationState(t.Body, t.ETag)
rt, err = getActivationState(t.Body)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func (t *CampaignContainersManager) UpsertState(ctx context.Context, name string
Campaign, err := t.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
itemState, err := getCampaignContainerState(Campaign.Body, Campaign.ETag)
itemState, err := getCampaignContainerState(Campaign.Body)
if err != nil {
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta)
}

body := map[string]interface{}{
Expand All @@ -109,7 +109,7 @@ func (t *CampaignContainersManager) UpsertState(ctx context.Context, name string
Value: states.StateEntry{
ID: name,
Body: body,
ETag: "",
ETag: state.ObjectMeta.ETag,
},
Metadata: map[string]interface{}{
"namespace": state.ObjectMeta.Namespace,
Expand Down Expand Up @@ -151,7 +151,7 @@ func (t *CampaignContainersManager) ListState(ctx context.Context, namespace str
ret := make([]model.CampaignContainerState, 0)
for _, t := range campaigncontainers {
var rt model.CampaignContainerState
rt, err = getCampaignContainerState(t.Body, t.ETag)
rt, err = getCampaignContainerState(t.Body)
if err != nil {
return nil, err
}
Expand All @@ -160,7 +160,7 @@ func (t *CampaignContainersManager) ListState(ctx context.Context, namespace str
return ret, nil
}

func getCampaignContainerState(body interface{}, etag string) (model.CampaignContainerState, error) {
func getCampaignContainerState(body interface{}) (model.CampaignContainerState, error) {
var CampaignContainerState model.CampaignContainerState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &CampaignContainerState)
Expand All @@ -170,7 +170,6 @@ func getCampaignContainerState(body interface{}, etag string) (model.CampaignCon
if CampaignContainerState.Spec == nil {
CampaignContainerState.Spec = &model.CampaignContainerSpec{}
}
CampaignContainerState.ObjectMeta.ETag = etag
return CampaignContainerState, nil
}

Expand Down Expand Up @@ -198,7 +197,7 @@ func (t *CampaignContainersManager) GetState(ctx context.Context, id string, nam
return model.CampaignContainerState{}, err
}
var ret model.CampaignContainerState
ret, err = getCampaignContainerState(Campaign.Body, Campaign.ETag)
ret, err = getCampaignContainerState(Campaign.Body)
if err != nil {
return model.CampaignContainerState{}, err
}
Expand Down
12 changes: 6 additions & 6 deletions api/pkg/apis/v1alpha1/managers/campaigns/campaigns-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func (m *CampaignsManager) GetState(ctx context.Context, name string, namespace
return model.CampaignState{}, err
}
var ret model.CampaignState
ret, err = getCampaignState(entry.Body, entry.ETag)
ret, err = getCampaignState(entry.Body)
if err != nil {
log.ErrorfCtx(ctx, "Failed to convert to campaign state for %s in namespace %s: %v", name, namespace, err)
return model.CampaignState{}, err
}
return ret, nil
}

func getCampaignState(body interface{}, etag string) (model.CampaignState, error) {
func getCampaignState(body interface{}) (model.CampaignState, error) {
var campaignState model.CampaignState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &campaignState)
Expand All @@ -98,7 +98,6 @@ func getCampaignState(body interface{}, etag string) (model.CampaignState, error
if campaignState.Spec == nil {
campaignState.Spec = &model.CampaignSpec{}
}
campaignState.ObjectMeta.ETag = etag
return campaignState, nil
}

Expand Down Expand Up @@ -129,12 +128,12 @@ func (m *CampaignsManager) UpsertState(ctx context.Context, name string, state m
entry, err := m.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
itemState, err := getCampaignState(entry.Body, entry.ETag)
itemState, err := getCampaignState(entry.Body)
if err != nil {
log.ErrorfCtx(ctx, "Failed to convert to campaign state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err)
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta)
}

if m.needValidate {
Expand All @@ -158,6 +157,7 @@ func (m *CampaignsManager) UpsertState(ctx context.Context, name string, state m
"metadata": state.ObjectMeta,
"spec": state.Spec,
},
ETag: state.ObjectMeta.ETag,
},
Metadata: map[string]interface{}{
"namespace": state.ObjectMeta.Namespace,
Expand Down Expand Up @@ -225,7 +225,7 @@ func (t *CampaignsManager) ListState(ctx context.Context, namespace string) ([]m
ret := make([]model.CampaignState, 0)
for _, t := range campaigns {
var rt model.CampaignState
rt, err = getCampaignState(t.Body, t.ETag)
rt, err = getCampaignState(t.Body)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func (t *CatalogContainersManager) UpsertState(ctx context.Context, name string,
item, err := t.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
itemState, err := getCatalogContainerState(item.Body, item.ETag)
itemState, err := getCatalogContainerState(item.Body)
if err != nil {
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta)
}

body := map[string]interface{}{
Expand All @@ -109,7 +109,7 @@ func (t *CatalogContainersManager) UpsertState(ctx context.Context, name string,
Value: states.StateEntry{
ID: name,
Body: body,
ETag: "",
ETag: state.ObjectMeta.ETag,
},
Metadata: map[string]interface{}{
"namespace": state.ObjectMeta.Namespace,
Expand Down Expand Up @@ -151,7 +151,7 @@ func (t *CatalogContainersManager) ListState(ctx context.Context, namespace stri
ret := make([]model.CatalogContainerState, 0)
for _, t := range catalogcontainers {
var rt model.CatalogContainerState
rt, err = getCatalogContainerState(t.Body, t.ETag)
rt, err = getCatalogContainerState(t.Body)
if err != nil {
return nil, err
}
Expand All @@ -160,7 +160,7 @@ func (t *CatalogContainersManager) ListState(ctx context.Context, namespace stri
return ret, nil
}

func getCatalogContainerState(body interface{}, etag string) (model.CatalogContainerState, error) {
func getCatalogContainerState(body interface{}) (model.CatalogContainerState, error) {
var CatalogContainerState model.CatalogContainerState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &CatalogContainerState)
Expand All @@ -170,7 +170,6 @@ func getCatalogContainerState(body interface{}, etag string) (model.CatalogConta
if CatalogContainerState.Spec == nil {
CatalogContainerState.Spec = &model.CatalogContainerSpec{}
}
CatalogContainerState.ObjectMeta.ETag = etag
return CatalogContainerState, nil
}

Expand Down Expand Up @@ -198,7 +197,7 @@ func (t *CatalogContainersManager) GetState(ctx context.Context, id string, name
return model.CatalogContainerState{}, err
}
var ret model.CatalogContainerState
ret, err = getCatalogContainerState(Campaign.Body, Campaign.ETag)
ret, err = getCatalogContainerState(Campaign.Body)
if err != nil {
return model.CatalogContainerState{}, err
}
Expand Down
11 changes: 5 additions & 6 deletions api/pkg/apis/v1alpha1/managers/catalogs/catalogs-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ func (s *CatalogsManager) GetState(ctx context.Context, name string, namespace s
return model.CatalogState{}, err
}
var ret model.CatalogState
ret, err = getCatalogState(entry.Body, entry.ETag)
ret, err = getCatalogState(entry.Body)
if err != nil {
return model.CatalogState{}, err
}
return ret, nil
}

func getCatalogState(body interface{}, etag string) (model.CatalogState, error) {
func getCatalogState(body interface{}) (model.CatalogState, error) {
var catalogState model.CatalogState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &catalogState)
Expand All @@ -102,7 +102,6 @@ func getCatalogState(body interface{}, etag string) (model.CatalogState, error)
if catalogState.Spec == nil {
catalogState.Spec = &model.CatalogSpec{}
}
catalogState.ObjectMeta.ETag = etag
if catalogState.Status == nil {
catalogState.Status = &model.CatalogStatus{}
}
Expand Down Expand Up @@ -135,11 +134,11 @@ func (m *CatalogsManager) UpsertState(ctx context.Context, name string, state mo
entry, err := m.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
itemState, err := getCatalogState(entry.Body, entry.ETag)
itemState, err := getCatalogState(entry.Body)
if err != nil {
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta)
}

if m.needValidate {
Expand Down Expand Up @@ -249,7 +248,7 @@ func (t *CatalogsManager) ListState(ctx context.Context, namespace string, filte
ret := make([]model.CatalogState, 0)
for _, t := range catalogs {
var rt model.CatalogState
rt, err = getCatalogState(t.Body, t.ETag)
rt, err = getCatalogState(t.Body)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions api/pkg/apis/v1alpha1/managers/devices/devices-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ func (t *DevicesManager) UpsertState(ctx context.Context, name string, state mod
entry, err := t.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
ret, err := getDeviceState(entry.Body, entry.ETag)
ret, err := getDeviceState(entry.Body)
if err != nil {
log.ErrorfCtx(ctx, " M (Devices): GetSpec failed to get device state, error: %v", err)
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(ret.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(ret.ObjectMeta)
}

upsertRequest := states.UpsertRequest{
Expand All @@ -115,6 +115,7 @@ func (t *DevicesManager) UpsertState(ctx context.Context, name string, state mod
"metadata": state.ObjectMeta,
"spec": state.Spec,
},
ETag: state.ObjectMeta.ETag,
},
Metadata: map[string]interface{}{
"namespace": state.ObjectMeta.Namespace,
Expand Down Expand Up @@ -159,7 +160,7 @@ func (t *DevicesManager) ListState(ctx context.Context, namespace string) ([]mod
ret := make([]model.DeviceState, 0)
for _, t := range devices {
var rt model.DeviceState
rt, err = getDeviceState(t.Body, t.ETag)
rt, err = getDeviceState(t.Body)
if err != nil {
log.ErrorfCtx(ctx, " M (Devices): ListState failed to get device state %s, error: %v", t.ID, err)
return nil, err
Expand All @@ -169,7 +170,7 @@ func (t *DevicesManager) ListState(ctx context.Context, namespace string) ([]mod
return ret, nil
}

func getDeviceState(body interface{}, etag string) (model.DeviceState, error) {
func getDeviceState(body interface{}) (model.DeviceState, error) {
var deviceState model.DeviceState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &deviceState)
Expand All @@ -179,7 +180,6 @@ func getDeviceState(body interface{}, etag string) (model.DeviceState, error) {
if deviceState.Spec == nil {
deviceState.Spec = &model.DeviceSpec{}
}
deviceState.ObjectMeta.ETag = etag
return deviceState, nil
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (t *DevicesManager) GetState(ctx context.Context, name string, namespace st
return model.DeviceState{}, err
}
var ret model.DeviceState
ret, err = getDeviceState(entry.Body, entry.ETag)
ret, err = getDeviceState(entry.Body)
if err != nil {
log.ErrorfCtx(ctx, " M (Devices): GetSpec failed to get device state, error: %v", err)
return model.DeviceState{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func TestUpsertAndGet(t *testing.T) {
ObjectMeta: model.ObjectMeta{
Name: "test",
Namespace: "default",
ETag: "1",
},
Spec: &model.DeviceSpec{
DisplayName: "device",
Expand Down
11 changes: 5 additions & 6 deletions api/pkg/apis/v1alpha1/managers/instances/instances-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func (t *InstancesManager) UpsertState(ctx context.Context, name string, state m
item, err := t.StateProvider.Get(ctx, getRequest)
if err == nil {
// preserve system annotations for existing object
itemState, err := getInstanceState(item.Body, item.ETag)
itemState, err := getInstanceState(item.Body)
if err != nil {
log.ErrorfCtx(ctx, "Failed to convert to instance state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err)
return err
}
state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations)
state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta)
}

if t.needValidate {
Expand Down Expand Up @@ -177,7 +177,7 @@ func (t *InstancesManager) ListState(ctx context.Context, namespace string) ([]m
ret := make([]model.InstanceState, 0)
for _, t := range instances {
var rt model.InstanceState
rt, err = getInstanceState(t.Body, t.ETag)
rt, err = getInstanceState(t.Body)
if err != nil {
return nil, err
}
Expand All @@ -186,7 +186,7 @@ func (t *InstancesManager) ListState(ctx context.Context, namespace string) ([]m
return ret, nil
}

func getInstanceState(body interface{}, etag string) (model.InstanceState, error) {
func getInstanceState(body interface{}) (model.InstanceState, error) {
var instanceState model.InstanceState
bytes, _ := json.Marshal(body)
err := json.Unmarshal(bytes, &instanceState)
Expand All @@ -196,7 +196,6 @@ func getInstanceState(body interface{}, etag string) (model.InstanceState, error
if instanceState.Spec == nil {
instanceState.Spec = &model.InstanceSpec{}
}
instanceState.ObjectMeta.ETag = etag
return instanceState, nil
}

Expand Down Expand Up @@ -224,7 +223,7 @@ func (t *InstancesManager) GetState(ctx context.Context, id string, namespace st
return model.InstanceState{}, err
}
var ret model.InstanceState
ret, err = getInstanceState(instance.Body, instance.ETag)
ret, err = getInstanceState(instance.Body)
if err != nil {
return model.InstanceState{}, err
}
Expand Down
Loading

0 comments on commit 9e7725f

Please sign in to comment.