How to use Connect method of state Package

Best Syzkaller code snippet using state.Connect

connect_ca.go

Source:connect_ca.go Github

copy

Full Screen

...4 "github.com/hashicorp/go-memdb"5 "github.com/hashicorp/consul/agent/structs"6)7const (8 tableConnectCABuiltin = "connect-ca-builtin"9 tableConnectCABuiltinSerial = "connect-ca-builtin-serial"10 tableConnectCAConfig = "connect-ca-config"11 tableConnectCARoots = "connect-ca-roots"12 tableConnectCALeafCerts = "connect-ca-leaf-certs"13)14// caBuiltinProviderTableSchema returns a new table schema used for storing15// the built-in CA provider's state for connect. This is only used by16// the internal Consul CA provider.17func caBuiltinProviderTableSchema() *memdb.TableSchema {18 return &memdb.TableSchema{19 Name: tableConnectCABuiltin,20 Indexes: map[string]*memdb.IndexSchema{21 "id": {22 Name: "id",23 AllowMissing: false,24 Unique: true,25 Indexer: &memdb.StringFieldIndex{26 Field: "ID",27 },28 },29 },30 }31}32// caConfigTableSchema returns a new table schema used for storing33// the CA config for Connect.34func caConfigTableSchema() *memdb.TableSchema {35 return &memdb.TableSchema{36 Name: tableConnectCAConfig,37 Indexes: map[string]*memdb.IndexSchema{38 // This table only stores one row, so this just ignores the ID field39 // and always overwrites the same config object.40 "id": {41 Name: "id",42 AllowMissing: true,43 Unique: true,44 Indexer: &memdb.ConditionalIndex{45 Conditional: func(obj interface{}) (bool, error) { return true, nil },46 },47 },48 },49 }50}51// caRootTableSchema returns a new table schema used for storing52// CA roots for Connect.53func caRootTableSchema() *memdb.TableSchema {54 return &memdb.TableSchema{55 Name: tableConnectCARoots,56 Indexes: map[string]*memdb.IndexSchema{57 "id": {58 Name: "id",59 AllowMissing: false,60 Unique: true,61 Indexer: &memdb.StringFieldIndex{62 Field: "ID",63 },64 },65 },66 }67}68// CAConfig is used to pull the CA config from the snapshot.69func (s *Snapshot) CAConfig() (*structs.CAConfiguration, error) {70 c, err := s.tx.First(tableConnectCAConfig, "id")71 if err != nil {72 return nil, err73 }74 config, ok := c.(*structs.CAConfiguration)75 if !ok {76 return nil, nil77 }78 return config, nil79}80// CAConfig is used when restoring from a snapshot.81func (s *Restore) CAConfig(config *structs.CAConfiguration) error {82 // Don't restore a blank CA config83 // https://github.com/hashicorp/consul/issues/495484 if config.Provider == "" {85 return nil86 }87 if err := s.tx.Insert(tableConnectCAConfig, config); err != nil {88 return fmt.Errorf("failed restoring CA config: %s", err)89 }90 return nil91}92// CAConfig is used to get the current CA configuration.93func (s *Store) CAConfig(ws memdb.WatchSet) (uint64, *structs.CAConfiguration, error) {94 tx := s.db.Txn(false)95 defer tx.Abort()96 return caConfigTxn(tx, ws)97}98func caConfigTxn(tx ReadTxn, ws memdb.WatchSet) (uint64, *structs.CAConfiguration, error) {99 // Get the CA config100 ch, c, err := tx.FirstWatch(tableConnectCAConfig, "id")101 if err != nil {102 return 0, nil, fmt.Errorf("failed CA config lookup: %s", err)103 }104 ws.Add(ch)105 config, ok := c.(*structs.CAConfiguration)106 if !ok {107 return 0, nil, nil108 }109 return config.ModifyIndex, config, nil110}111// CASetConfig is used to set the current CA configuration.112func (s *Store) CASetConfig(idx uint64, config *structs.CAConfiguration) error {113 tx := s.db.WriteTxn(idx)114 defer tx.Abort()115 if err := s.caSetConfigTxn(idx, tx, config); err != nil {116 return err117 }118 return tx.Commit()119}120// CACheckAndSetConfig is used to try updating the CA configuration with a121// given Raft index. If the CAS index specified is not equal to the last observed index122// for the config, then the call is a noop,123func (s *Store) CACheckAndSetConfig(idx, cidx uint64, config *structs.CAConfiguration) (bool, error) {124 tx := s.db.WriteTxn(idx)125 defer tx.Abort()126 // Check for an existing config127 existing, err := tx.First(tableConnectCAConfig, "id")128 if err != nil {129 return false, fmt.Errorf("failed CA config lookup: %s", err)130 }131 // If the existing index does not match the provided CAS132 // index arg, then we shouldn't update anything and can safely133 // return early here.134 e, ok := existing.(*structs.CAConfiguration)135 if (ok && e.ModifyIndex != cidx) || (!ok && cidx != 0) {136 return false, nil137 }138 if err := s.caSetConfigTxn(idx, tx, config); err != nil {139 return false, err140 }141 err = tx.Commit()142 return err == nil, err143}144func (s *Store) caSetConfigTxn(idx uint64, tx *txn, config *structs.CAConfiguration) error {145 // Check for an existing config146 prev, err := tx.First(tableConnectCAConfig, "id")147 if err != nil {148 return fmt.Errorf("failed CA config lookup: %s", err)149 }150 // Set the indexes, prevent the cluster ID from changing.151 if prev != nil {152 existing := prev.(*structs.CAConfiguration)153 config.CreateIndex = existing.CreateIndex154 // Allow the ClusterID to change if it's provided by an internal operation, such155 // as a primary datacenter being switched to secondary mode.156 if config.ClusterID == "" {157 config.ClusterID = existing.ClusterID158 }159 } else {160 config.CreateIndex = idx161 }162 config.ModifyIndex = idx163 if err := tx.Insert(tableConnectCAConfig, config); err != nil {164 return fmt.Errorf("failed updating CA config: %s", err)165 }166 return nil167}168// CARoots is used to pull all the CA roots for the snapshot.169func (s *Snapshot) CARoots() (structs.CARoots, error) {170 ixns, err := s.tx.Get(tableConnectCARoots, "id")171 if err != nil {172 return nil, err173 }174 var ret structs.CARoots175 for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {176 ret = append(ret, wrapped.(*structs.CARoot))177 }178 return ret, nil179}180// CARoots is used when restoring from a snapshot.181func (s *Restore) CARoot(r *structs.CARoot) error {182 // Insert183 if err := s.tx.Insert(tableConnectCARoots, r); err != nil {184 return fmt.Errorf("failed restoring CA root: %s", err)185 }186 if err := indexUpdateMaxTxn(s.tx, r.ModifyIndex, tableConnectCARoots); err != nil {187 return fmt.Errorf("failed updating index: %s", err)188 }189 return nil190}191// CARoots returns the list of all CA roots.192func (s *Store) CARoots(ws memdb.WatchSet) (uint64, structs.CARoots, error) {193 tx := s.db.Txn(false)194 defer tx.Abort()195 return caRootsTxn(tx, ws)196}197func caRootsTxn(tx ReadTxn, ws memdb.WatchSet) (uint64, structs.CARoots, error) {198 // Get the index199 idx := maxIndexTxn(tx, tableConnectCARoots)200 // Get all201 iter, err := tx.Get(tableConnectCARoots, "id")202 if err != nil {203 return 0, nil, fmt.Errorf("failed CA root lookup: %s", err)204 }205 ws.Add(iter.WatchCh())206 var results structs.CARoots207 for v := iter.Next(); v != nil; v = iter.Next() {208 results = append(results, v.(*structs.CARoot))209 }210 return idx, results, nil211}212// CARootActive returns the currently active CARoot.213func (s *Store) CARootActive(ws memdb.WatchSet) (uint64, *structs.CARoot, error) {214 // Get all the roots since there should never be that many and just215 // do the filtering in this method.216 var result *structs.CARoot217 idx, roots, err := s.CARoots(ws)218 if err == nil {219 for _, r := range roots {220 if r.Active {221 result = r222 break223 }224 }225 }226 return idx, result, err227}228// CARootSetCAS sets the current CA root state using a check-and-set operation.229// On success, this will replace the previous set of CARoots completely with230// the given set of roots.231//232// The first boolean result returns whether the transaction succeeded or not.233func (s *Store) CARootSetCAS(idx, cidx uint64, rs []*structs.CARoot) (bool, error) {234 tx := s.db.WriteTxn(idx)235 defer tx.Abort()236 // There must be exactly one active CA root.237 activeCount := 0238 for _, r := range rs {239 if r.Active {240 activeCount++241 }242 }243 if activeCount != 1 {244 return false, fmt.Errorf("there must be exactly one active CA")245 }246 // Get the current max index247 if midx := maxIndexTxn(tx, tableConnectCARoots); midx != cidx {248 return false, nil249 }250 // Go through and find any existing matching CAs so we can preserve and251 // update their Create/ModifyIndex values.252 for _, r := range rs {253 if r.ID == "" {254 return false, ErrMissingCARootID255 }256 existing, err := tx.First(tableConnectCARoots, "id", r.ID)257 if err != nil {258 return false, fmt.Errorf("failed CA root lookup: %s", err)259 }260 if existing != nil {261 r.CreateIndex = existing.(*structs.CARoot).CreateIndex262 } else {263 r.CreateIndex = idx264 }265 r.ModifyIndex = idx266 }267 // Delete all268 _, err := tx.DeleteAll(tableConnectCARoots, "id")269 if err != nil {270 return false, err271 }272 // Insert all273 for _, r := range rs {274 if err := tx.Insert(tableConnectCARoots, r); err != nil {275 return false, err276 }277 }278 // Update the index279 if err := tx.Insert("index", &IndexEntry{tableConnectCARoots, idx}); err != nil {280 return false, fmt.Errorf("failed updating index: %s", err)281 }282 err = tx.Commit()283 return err == nil, err284}285// CAProviderState is used to pull the built-in provider states from the snapshot.286func (s *Snapshot) CAProviderState() ([]*structs.CAConsulProviderState, error) {287 ixns, err := s.tx.Get(tableConnectCABuiltin, "id")288 if err != nil {289 return nil, err290 }291 var ret []*structs.CAConsulProviderState292 for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {293 ret = append(ret, wrapped.(*structs.CAConsulProviderState))294 }295 return ret, nil296}297// CAProviderState is used when restoring from a snapshot.298func (s *Restore) CAProviderState(state *structs.CAConsulProviderState) error {299 if err := s.tx.Insert(tableConnectCABuiltin, state); err != nil {300 return fmt.Errorf("failed restoring built-in CA state: %s", err)301 }302 if err := indexUpdateMaxTxn(s.tx, state.ModifyIndex, tableConnectCABuiltin); err != nil {303 return fmt.Errorf("failed updating index: %s", err)304 }305 return nil306}307// CAProviderState is used to get the Consul CA provider state for the given ID.308func (s *Store) CAProviderState(id string) (uint64, *structs.CAConsulProviderState, error) {309 tx := s.db.Txn(false)310 defer tx.Abort()311 // Get the index312 idx := maxIndexTxn(tx, tableConnectCABuiltin)313 // Get the provider config314 c, err := tx.First(tableConnectCABuiltin, "id", id)315 if err != nil {316 return 0, nil, fmt.Errorf("failed built-in CA state lookup: %s", err)317 }318 state, ok := c.(*structs.CAConsulProviderState)319 if !ok {320 return 0, nil, nil321 }322 return idx, state, nil323}324// CASetProviderState is used to set the current built-in CA provider state.325func (s *Store) CASetProviderState(idx uint64, state *structs.CAConsulProviderState) (bool, error) {326 tx := s.db.WriteTxn(idx)327 defer tx.Abort()328 // Check for an existing config329 existing, err := tx.First(tableConnectCABuiltin, "id", state.ID)330 if err != nil {331 return false, fmt.Errorf("failed built-in CA state lookup: %s", err)332 }333 // Set the indexes.334 if existing != nil {335 state.CreateIndex = existing.(*structs.CAConsulProviderState).CreateIndex336 } else {337 state.CreateIndex = idx338 }339 state.ModifyIndex = idx340 if err := tx.Insert(tableConnectCABuiltin, state); err != nil {341 return false, fmt.Errorf("failed updating built-in CA state: %s", err)342 }343 // Update the index344 if err := tx.Insert("index", &IndexEntry{tableConnectCABuiltin, idx}); err != nil {345 return false, fmt.Errorf("failed updating index: %s", err)346 }347 err = tx.Commit()348 return err == nil, err349}350// CADeleteProviderState is used to remove the built-in Consul CA provider351// state for the given ID.352func (s *Store) CADeleteProviderState(idx uint64, id string) error {353 tx := s.db.WriteTxn(idx)354 defer tx.Abort()355 // Check for an existing config356 existing, err := tx.First(tableConnectCABuiltin, "id", id)357 if err != nil {358 return fmt.Errorf("failed built-in CA state lookup: %s", err)359 }360 if existing == nil {361 return nil362 }363 providerState := existing.(*structs.CAConsulProviderState)364 // Do the delete and update the index365 if err := tx.Delete(tableConnectCABuiltin, providerState); err != nil {366 return err367 }368 if err := tx.Insert("index", &IndexEntry{tableConnectCABuiltin, idx}); err != nil {369 return fmt.Errorf("failed updating index: %s", err)370 }371 return tx.Commit()372}373func (s *Store) CALeafSetIndex(idx uint64, index uint64) error {374 tx := s.db.WriteTxn(idx)375 defer tx.Abort()376 return indexUpdateMaxTxn(tx, index, tableConnectCALeafCerts)377}378func (s *Store) CARootsAndConfig(ws memdb.WatchSet) (uint64, structs.CARoots, *structs.CAConfiguration, error) {379 tx := s.db.Txn(false)380 defer tx.Abort()381 confIdx, config, err := caConfigTxn(tx, ws)382 if err != nil {383 return 0, nil, nil, fmt.Errorf("failed CA config lookup: %v", err)384 }385 rootsIdx, roots, err := caRootsTxn(tx, ws)386 if err != nil {387 return 0, nil, nil, fmt.Errorf("failed CA roots lookup: %v", err)388 }389 idx := rootsIdx390 if confIdx > idx {391 idx = confIdx392 }393 return idx, roots, config, nil394}395func (s *Store) CAIncrementProviderSerialNumber(idx uint64) (uint64, error) {396 tx := s.db.WriteTxn(idx)397 defer tx.Abort()398 existing, err := tx.First("index", "id", tableConnectCABuiltinSerial)399 if err != nil {400 return 0, fmt.Errorf("failed built-in CA serial number lookup: %s", err)401 }402 var last uint64403 if existing != nil {404 last = existing.(*IndexEntry).Value405 } else {406 // Serials used to be based on the raft indexes in the provider table,407 // so bootstrap off of that.408 last = maxIndexTxn(tx, tableConnectCABuiltin)409 }410 next := last + 1411 if err := tx.Insert("index", &IndexEntry{tableConnectCABuiltinSerial, next}); err != nil {412 return 0, fmt.Errorf("failed updating index: %s", err)413 }414 err = tx.Commit()415 return next, err416}...

Full Screen

Full Screen

finder.go

Source:finder.go Github

copy

Full Screen

...5 "github.com/hashicorp/aws-sdk-go-base/tfawserr"6 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"7 "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"8)9func ConnectionByID(conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) {10 input := &directconnect.DescribeConnectionsInput{11 ConnectionId: aws.String(id),12 }13 output, err := conn.DescribeConnections(input)14 if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Connection with ID") {15 return nil, &resource.NotFoundError{16 LastError: err,17 LastRequest: input,18 }19 }20 if err != nil {21 return nil, err22 }23 if output == nil || len(output.Connections) == 0 || output.Connections[0] == nil {24 return nil, tfresource.NewEmptyResultError(input)25 }26 if count := len(output.Connections); count > 1 {27 return nil, tfresource.NewTooManyResultsError(count, input)28 }29 connection := output.Connections[0]30 if state := aws.StringValue(connection.ConnectionState); state == directconnect.ConnectionStateDeleted || state == directconnect.ConnectionStateRejected {31 return nil, &resource.NotFoundError{32 Message: state,33 LastRequest: input,34 }35 }36 return connection, nil37}38func ConnectionAssociationExists(conn *directconnect.DirectConnect, connectionID, lagID string) error {39 connection, err := ConnectionByID(conn, connectionID)40 if err != nil {41 return err42 }43 if lagID != aws.StringValue(connection.LagId) {44 return &resource.NotFoundError{}45 }46 return nil47}48func GatewayByID(conn *directconnect.DirectConnect, id string) (*directconnect.Gateway, error) {49 input := &directconnect.DescribeDirectConnectGatewaysInput{50 DirectConnectGatewayId: aws.String(id),51 }52 output, err := conn.DescribeDirectConnectGateways(input)53 if err != nil {54 return nil, err55 }56 if output == nil || len(output.DirectConnectGateways) == 0 || output.DirectConnectGateways[0] == nil {57 return nil, tfresource.NewEmptyResultError(input)58 }59 if count := len(output.DirectConnectGateways); count > 1 {60 return nil, tfresource.NewTooManyResultsError(count, input)61 }62 gateway := output.DirectConnectGateways[0]63 if state := aws.StringValue(gateway.DirectConnectGatewayState); state == directconnect.GatewayStateDeleted {64 return nil, &resource.NotFoundError{65 Message: state,66 LastRequest: input,67 }68 }69 return gateway, nil70}71func GatewayAssociationByID(conn *directconnect.DirectConnect, id string) (*directconnect.GatewayAssociation, error) {72 input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{73 AssociationId: aws.String(id),74 }75 return GatewayAssociation(conn, input)76}77func GatewayAssociationByDirectConnectGatewayIDAndAssociatedGatewayID(conn *directconnect.DirectConnect, directConnectGatewayID, associatedGatewayID string) (*directconnect.GatewayAssociation, error) {78 input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{79 AssociatedGatewayId: aws.String(associatedGatewayID),80 DirectConnectGatewayId: aws.String(directConnectGatewayID),81 }82 return GatewayAssociation(conn, input)83}84func GatewayAssociationByDirectConnectGatewayIDAndVirtualGatewayID(conn *directconnect.DirectConnect, directConnectGatewayID, virtualGatewayID string) (*directconnect.GatewayAssociation, error) {85 input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{86 DirectConnectGatewayId: aws.String(directConnectGatewayID),87 VirtualGatewayId: aws.String(virtualGatewayID),88 }89 return GatewayAssociation(conn, input)90}91func GatewayAssociation(conn *directconnect.DirectConnect, input *directconnect.DescribeDirectConnectGatewayAssociationsInput) (*directconnect.GatewayAssociation, error) {92 output, err := conn.DescribeDirectConnectGatewayAssociations(input)93 if err != nil {94 return nil, err95 }96 if output == nil || len(output.DirectConnectGatewayAssociations) == 0 || output.DirectConnectGatewayAssociations[0] == nil {97 return nil, tfresource.NewEmptyResultError(input)98 }99 if count := len(output.DirectConnectGatewayAssociations); count > 1 {100 return nil, tfresource.NewTooManyResultsError(count, input)101 }102 association := output.DirectConnectGatewayAssociations[0]103 if state := aws.StringValue(association.AssociationState); state == directconnect.GatewayAssociationStateDisassociated {104 return nil, &resource.NotFoundError{105 Message: state,106 LastRequest: input,107 }108 }109 if association.AssociatedGateway == nil {110 return nil, &resource.NotFoundError{111 Message: "Empty AssociatedGateway",112 LastRequest: input,113 }114 }115 return association, nil116}117func GatewayAssociationProposalByID(conn *directconnect.DirectConnect, id string) (*directconnect.GatewayAssociationProposal, error) {118 input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{119 ProposalId: aws.String(id),120 }121 output, err := conn.DescribeDirectConnectGatewayAssociationProposals(input)122 if err != nil {123 return nil, err124 }125 if output == nil || len(output.DirectConnectGatewayAssociationProposals) == 0 || output.DirectConnectGatewayAssociationProposals[0] == nil {126 return nil, tfresource.NewEmptyResultError(input)127 }128 if count := len(output.DirectConnectGatewayAssociationProposals); count > 1 {129 return nil, tfresource.NewTooManyResultsError(count, input)130 }131 proposal := output.DirectConnectGatewayAssociationProposals[0]132 if state := aws.StringValue(proposal.ProposalState); state == directconnect.GatewayAssociationProposalStateDeleted {133 return nil, &resource.NotFoundError{134 Message: state,135 LastRequest: input,136 }137 }138 if proposal.AssociatedGateway == nil {139 return nil, &resource.NotFoundError{140 Message: "Empty AssociatedGateway",141 LastRequest: input,142 }143 }144 return proposal, nil145}146func HostedConnectionByID(conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) {147 input := &directconnect.DescribeHostedConnectionsInput{148 ConnectionId: aws.String(id),149 }150 output, err := conn.DescribeHostedConnections(input)151 if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Connection with ID") {152 return nil, &resource.NotFoundError{153 LastError: err,154 LastRequest: input,155 }156 }157 if err != nil {158 return nil, err159 }160 if output == nil || len(output.Connections) == 0 || output.Connections[0] == nil {161 return nil, tfresource.NewEmptyResultError(input)162 }163 if count := len(output.Connections); count > 1 {164 return nil, tfresource.NewTooManyResultsError(count, input)165 }166 connection := output.Connections[0]167 if state := aws.StringValue(connection.ConnectionState); state == directconnect.ConnectionStateDeleted || state == directconnect.ConnectionStateRejected {168 return nil, &resource.NotFoundError{169 Message: state,170 LastRequest: input,171 }172 }173 return connection, nil174}175func LagByID(conn *directconnect.DirectConnect, id string) (*directconnect.Lag, error) {176 input := &directconnect.DescribeLagsInput{177 LagId: aws.String(id),178 }179 output, err := conn.DescribeLags(input)180 if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Lag with ID") {181 return nil, &resource.NotFoundError{182 LastError: err,183 LastRequest: input,184 }185 }186 if err != nil {187 return nil, err188 }189 if output == nil || len(output.Lags) == 0 || output.Lags[0] == nil {190 return nil, tfresource.NewEmptyResultError(input)191 }192 if count := len(output.Lags); count > 1 {193 return nil, tfresource.NewTooManyResultsError(count, input)194 }195 lag := output.Lags[0]196 if state := aws.StringValue(lag.LagState); state == directconnect.LagStateDeleted {197 return nil, &resource.NotFoundError{198 Message: state,199 LastRequest: input,200 }201 }202 return lag, nil203}204func LocationByCode(conn *directconnect.DirectConnect, code string) (*directconnect.Location, error) {205 input := &directconnect.DescribeLocationsInput{}206 locations, err := Locations(conn, input)207 if err != nil {208 return nil, err209 }210 for _, location := range locations {211 if aws.StringValue(location.LocationCode) == code {212 return location, nil213 }214 }215 return nil, tfresource.NewEmptyResultError(input)216}217func Locations(conn *directconnect.DirectConnect, input *directconnect.DescribeLocationsInput) ([]*directconnect.Location, error) {218 output, err := conn.DescribeLocations(input)219 if err != nil {220 return nil, err221 }222 if output == nil || len(output.Locations) == 0 {223 return nil, tfresource.NewEmptyResultError(input)224 }225 return output.Locations, nil226}...

Full Screen

Full Screen

tasks_test.go

Source:tasks_test.go Github

copy

Full Screen

1package manager2import (3 "testing"4 "github.com/90poe/connectctl/pkg/client/connect"5 "github.com/stretchr/testify/assert"6)7func TestTasks_ByID_Found(t *testing.T) {8 f := ByID(3, 2, 1)9 assert.True(t, f(connect.TaskState{ID: 1}))10 assert.True(t, f(connect.TaskState{ID: 2}))11 assert.True(t, f(connect.TaskState{ID: 3}))12 assert.False(t, f(connect.TaskState{ID: 0}))13 assert.False(t, f(connect.TaskState{ID: 4}))14}15func TestTasks_IsRunning(t *testing.T) {16 f := IsRunning17 assert.True(t, f(connect.TaskState{State: "RUNNING"}))18 assert.False(t, f(connect.TaskState{State: "OTHER"}))19}20func TestTasks_IsNotRunning(t *testing.T) {21 f := IsNotRunning22 assert.False(t, f(connect.TaskState{State: "RUNNING"}))23 assert.True(t, f(connect.TaskState{State: "OTHER"}))24}25func TestTasks_IDsEmpty(t *testing.T) {26 var tasks Tasks27 assert.Equal(t, []int{}, tasks.IDs())28}29func TestTasks_IDsNotEmpty(t *testing.T) {30 assert.Equal(t, []int{1, 2, 3},31 Tasks{32 connect.TaskState{ID: 1},33 connect.TaskState{ID: 2},34 connect.TaskState{ID: 3},35 }.IDs())36}37func TestTasks_Filter(t *testing.T) {38 assert.Equal(t, Tasks{39 connect.TaskState{ID: 2},40 connect.TaskState{ID: 1},41 connect.TaskState{ID: 0},42 },43 Tasks{44 connect.TaskState{ID: -2},45 connect.TaskState{ID: 2},46 connect.TaskState{ID: -1},47 connect.TaskState{ID: 1},48 connect.TaskState{ID: 0},49 }.Filter(func(task connect.TaskState) bool {50 return task.ID >= 051 }))52}...

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1import (2var myLogger = logging.MustGetLogger("main")3type SimpleChaincode struct {4}5type State struct {6}7type City struct {8}9type User struct {10}11func main() {12 err := shim.Start(new(SimpleChaincode))13 if err != nil {14 fmt.Printf("Error starting Simple chaincode: %s", err)15 }16}

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {2 myLogger.Info("########### example_cc0 Init ###########")3 _, args := stub.GetFunctionAndParameters()4 userAsBytes, _ := json.Marshal(user)5 err := stub.PutState("1", userAsBytes)6 if err != nil {7 return shim.Error(err.Error())8 }9 return shim.Success(nil)10}

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {2 myLogger.Info("########### example_cc0 Invoke ###########")3 function, args := stub.GetFunctionAndParameters()4 if function == "createUser" {5 return t.createUser(stub, args)6 } else if function == "queryUser" {

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 sdl.Init(sdl.INIT_EVERYTHING)4 window, err := sdl.CreateWindow("Test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN)5 if err != nil {6 fmt.Println(err)7 }8 defer window.Destroy()9 renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)10 if err != nil {11 fmt.Println(err)12 }13 defer renderer.Destroy()14 texture, err := renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, 800, 600)15 if err != nil {16 fmt.Println(err)17 }18 defer texture.Destroy()19 pixels := make([]byte, 800*600*4)20 for running {21 for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {22 switch event.(type) {23 }24 }25 for i := 0; i < 800*600; i++ {26 }27 texture.Update(nil, pixels, 800*4)28 renderer.Clear()29 renderer.Copy(texture, nil, nil)30 renderer.Present()31 }32 sdl.Quit()33}34import (

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := state.Connect()4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(db)8}9import (10func main() {11 db, err := state.Connect()12 if err != nil {13 fmt.Println(err)14 }15 err = state.Insert(db, "California", "Sacramento")16 if err != nil {17 fmt.Println(err)18 }19}20import (21func main() {22 db, err := state.Connect()23 if err != nil {24 fmt.Println(err)25 }26 rows, err := state.Query(db)27 if err != nil {28 fmt.Println(err)29 }30 defer rows.Close()31 for rows.Next() {32 err = rows.Scan(&name, &capital)33 if err != nil {34 fmt.Println(err)35 }36 fmt.Println(name, capital)37 }38 err = rows.Err()39 if err != nil {40 fmt.Println(err)41 }42}43import (44func main() {45 db, err := state.Connect()46 if err != nil {47 fmt.Println(err)48 }49 err = state.Update(db, "California", "Sacramento")50 if err != nil {51 fmt.Println(err)52 }53}54import (55func main() {56 db, err := state.Connect()57 if err != nil {58 fmt.Println(err)59 }60 err = state.Delete(db, "California")61 if err != nil {62 fmt.Println(err)63 }64}65import (66func main() {

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := gographviz.NewGraph()4 err := g.SetName("G")5 if err != nil {6 panic(err)7 }8 err = g.SetDir(true)9 if err != nil {10 panic(err)11 }12 g.AddNode("G", "A", nil)13 g.AddNode("G", "B", nil)14 g.AddNode("G", "C", nil)15 g.AddEdge("A", "B", true, nil)16 g.AddEdge("A", "C", true, nil)17 g.AddEdge("B", "C", true, nil)18 g.AddEdge("C", "A", true, nil)19 p := gographviz.NewParser()20 p.SetDir(true)21 err = p.ParseString(g, `digraph G { A -> B -> C -> A }`)22 if err != nil {23 panic(err)24 }25 w := gographviz.NewWriter()26 err = w.Write(os.Stdout, g)27 if err != nil {28 panic(err)29 }30 w = gographviz.NewWriter()31 buf := filebuffer.New(nil)32 err = w.Write(buf, g)33 if err != nil {34 panic(err)35 }36 io.Copy(os.Stdout, buf)37}38import (39func main() {40 g := gographviz.NewGraph()41 err := g.SetName("G")42 if err != nil {43 panic(err)44 }45 err = g.SetDir(true)46 if err != nil {47 panic(err)48 }49 g.AddNode("G", "A", nil)50 g.AddNode("

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 st := state.NewState()4 st.Connect()5 fmt.Println(st)6}

Full Screen

Full Screen

Connect

Using AI Code Generation

copy

Full Screen

1func main() {2 state := State{}3 state.Connect("California")4 fmt.Println(state.GetState())5}6func main() {7 state := State{}8 state.Connect("California")9 fmt.Println(state.GetState())10}11func main() {12 state := State{}13 state.Connect("California")14 fmt.Println(state.GetState())15}16func main() {17 state := State{}18 state.Connect("California")19 fmt.Println(state.GetState())20}21func main() {22 state := State{}23 state.Connect("California")24 fmt.Println(state.GetState())25}26func main() {27 state := State{}28 state.Connect("California")29 fmt.Println(state.GetState())30}31func main() {32 state := State{}33 state.Connect("California")34 fmt.Println(state.GetState())35}36func main() {37 state := State{}38 state.Connect("California")39 fmt.Println(state.GetState())40}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful