github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/backend/remote-state/inmem/client.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package inmem
     5  
     6  import (
     7  	"crypto/md5"
     8  
     9  	"github.com/terramate-io/tf/states/remote"
    10  	"github.com/terramate-io/tf/states/statemgr"
    11  )
    12  
    13  // RemoteClient is a remote client that stores data in memory for testing.
    14  type RemoteClient struct {
    15  	Data []byte
    16  	MD5  []byte
    17  	Name string
    18  }
    19  
    20  func (c *RemoteClient) Get() (*remote.Payload, error) {
    21  	if c.Data == nil {
    22  		return nil, nil
    23  	}
    24  
    25  	return &remote.Payload{
    26  		Data: c.Data,
    27  		MD5:  c.MD5,
    28  	}, nil
    29  }
    30  
    31  func (c *RemoteClient) Put(data []byte) error {
    32  	md5 := md5.Sum(data)
    33  
    34  	c.Data = data
    35  	c.MD5 = md5[:]
    36  	return nil
    37  }
    38  
    39  func (c *RemoteClient) Delete() error {
    40  	c.Data = nil
    41  	c.MD5 = nil
    42  	return nil
    43  }
    44  
    45  func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) {
    46  	return locks.lock(c.Name, info)
    47  }
    48  func (c *RemoteClient) Unlock(id string) error {
    49  	return locks.unlock(c.Name, id)
    50  }