diff --git a/bootstrap/container/clients.go b/bootstrap/container/clients.go index 547c1a25..8b48b6aa 100644 --- a/bootstrap/container/clients.go +++ b/bootstrap/container/clients.go @@ -1,5 +1,6 @@ // // Copyright (c) 2022 Intel Corporation +// Copyright (C) 2024 IOTech Ltd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -179,3 +180,27 @@ func DeviceServiceCommandClientFrom(get di.Get) interfaces.DeviceServiceCommandC return client } + +// ScheduleJobClientName contains the name of the ScheduleJobClient's implementation in the DIC. +var ScheduleJobClientName = di.TypeInstanceToName((*interfaces.ScheduleJobClient)(nil)) + +// ScheduleJobClientFrom helper function queries the DIC and returns the ScheduleJobClient's implementation. +func ScheduleJobClientFrom(get di.Get) interfaces.ScheduleJobClient { + if get(ScheduleJobClientName) == nil { + return nil + } + + return get(ScheduleJobClientName).(interfaces.ScheduleJobClient) +} + +// ScheduleActionRecordClientName contains the name of the ScheduleActionRecordClient's implementation in the DIC. +var ScheduleActionRecordClientName = di.TypeInstanceToName((*interfaces.ScheduleActionRecordClient)(nil)) + +// ScheduleActionRecordClientFrom helper function queries the DIC and returns the ScheduleActionRecordClient's implementation. +func ScheduleActionRecordClientFrom(get di.Get) interfaces.ScheduleActionRecordClient { + if get(ScheduleActionRecordClientName) == nil { + return nil + } + + return get(ScheduleActionRecordClientName).(interfaces.ScheduleActionRecordClient) +} diff --git a/bootstrap/handlers/clients.go b/bootstrap/handlers/clients.go index a1a64e77..2a992a77 100644 --- a/bootstrap/handlers/clients.go +++ b/bootstrap/handlers/clients.go @@ -1,6 +1,7 @@ /******************************************************************************* * Copyright 2022 Intel Inc. * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 IOTech Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -175,6 +176,16 @@ func (cb *ClientsBootstrap) BootstrapHandler( }, }) + case common.SupportCronSchedulerServiceKey: + dic.Update(di.ServiceConstructorMap{ + container.ScheduleJobClientName: func(get di.Get) interface{} { + return clients.NewScheduleJobClient(url, jwtSecretProvider, cfg.GetBootstrap().Service.EnableNameFieldEscape) + }, + container.ScheduleActionRecordClientName: func(get di.Get) interface{} { + return clients.NewScheduleActionRecordClient(url, jwtSecretProvider, cfg.GetBootstrap().Service.EnableNameFieldEscape) + }, + }) + default: } diff --git a/bootstrap/handlers/clients_test.go b/bootstrap/handlers/clients_test.go index 27fdd505..fbd458ac 100644 --- a/bootstrap/handlers/clients_test.go +++ b/bootstrap/handlers/clients_test.go @@ -1,5 +1,6 @@ // // Copyright (c) 2022 Intel Corporation +// Copyright (C) 2024 IOTech Ltd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -77,12 +78,19 @@ func TestClientsBootstrapHandler(t *testing.T) { Protocol: "http", } + cronSchedulerClientInfo := config.ClientInfo{ + Host: "localhost", + Port: 59863, + Protocol: "http", + } + registryMock := ®istryMocks.Client{} registryMock.On("GetServiceEndpoint", common.CoreDataServiceKey).Return(types.ServiceEndpoint{}, nil) registryMock.On("GetServiceEndpoint", common.CoreMetaDataServiceKey).Return(types.ServiceEndpoint{}, nil) registryMock.On("GetServiceEndpoint", common.CoreCommandServiceKey).Return(types.ServiceEndpoint{}, nil) registryMock.On("GetServiceEndpoint", common.SupportNotificationsServiceKey).Return(types.ServiceEndpoint{}, nil) registryMock.On("GetServiceEndpoint", common.SupportSchedulerServiceKey).Return(types.ServiceEndpoint{}, nil) + registryMock.On("GetServiceEndpoint", common.SupportCronSchedulerServiceKey).Return(types.ServiceEndpoint{}, nil) registryErrorMock := ®istryMocks.Client{} registryErrorMock.On("GetServiceEndpoint", common.CoreDataServiceKey).Return(types.ServiceEndpoint{}, errors.New("some error")) @@ -90,84 +98,92 @@ func TestClientsBootstrapHandler(t *testing.T) { startupTimer := startup.NewTimer(1, 1) tests := []struct { - Name string - CoreDataClientInfo *config.ClientInfo - CommandClientInfo *config.ClientInfo - MetadataClientInfo *config.ClientInfo - NotificationClientInfo *config.ClientInfo - SchedulerClientInfo *config.ClientInfo - Registry registry.Client - ExpectedResult bool + Name string + CoreDataClientInfo *config.ClientInfo + CommandClientInfo *config.ClientInfo + MetadataClientInfo *config.ClientInfo + NotificationClientInfo *config.ClientInfo + SchedulerClientInfo *config.ClientInfo + CronSchedulerClientInfo *config.ClientInfo + Registry registry.Client + ExpectedResult bool }{ { - Name: "All ClientsBootstrap", - CoreDataClientInfo: &coreDataClientInfo, - CommandClientInfo: &commandHttpClientInfo, - MetadataClientInfo: &metadataClientInfo, - NotificationClientInfo: ¬ificationClientInfo, - SchedulerClientInfo: &subscriberClientInfo, - Registry: nil, - ExpectedResult: true, + Name: "All ClientsBootstrap", + CoreDataClientInfo: &coreDataClientInfo, + CommandClientInfo: &commandHttpClientInfo, + MetadataClientInfo: &metadataClientInfo, + NotificationClientInfo: ¬ificationClientInfo, + SchedulerClientInfo: &subscriberClientInfo, + CronSchedulerClientInfo: &cronSchedulerClientInfo, + Registry: nil, + ExpectedResult: true, }, { - Name: "All ClientsBootstrap using registry", - CoreDataClientInfo: &coreDataClientInfo, - CommandClientInfo: &commandHttpClientInfo, - MetadataClientInfo: &metadataClientInfo, - NotificationClientInfo: ¬ificationClientInfo, - SchedulerClientInfo: &subscriberClientInfo, - Registry: registryMock, - ExpectedResult: true, + Name: "All ClientsBootstrap using registry", + CoreDataClientInfo: &coreDataClientInfo, + CommandClientInfo: &commandHttpClientInfo, + MetadataClientInfo: &metadataClientInfo, + NotificationClientInfo: ¬ificationClientInfo, + SchedulerClientInfo: &subscriberClientInfo, + CronSchedulerClientInfo: &cronSchedulerClientInfo, + Registry: registryMock, + ExpectedResult: true, }, { - Name: "Core Data Client using registry fails", - CoreDataClientInfo: &coreDataClientInfo, - CommandClientInfo: nil, - MetadataClientInfo: nil, - NotificationClientInfo: nil, - SchedulerClientInfo: nil, - Registry: registryErrorMock, - ExpectedResult: false, + Name: "Core Data Client using registry fails", + CoreDataClientInfo: &coreDataClientInfo, + CommandClientInfo: nil, + MetadataClientInfo: nil, + NotificationClientInfo: nil, + SchedulerClientInfo: nil, + CronSchedulerClientInfo: nil, + Registry: registryErrorMock, + ExpectedResult: false, }, { - Name: "No ClientsBootstrap", - CoreDataClientInfo: nil, - CommandClientInfo: nil, - MetadataClientInfo: nil, - NotificationClientInfo: nil, - SchedulerClientInfo: nil, - Registry: nil, - ExpectedResult: true, + Name: "No ClientsBootstrap", + CoreDataClientInfo: nil, + CommandClientInfo: nil, + MetadataClientInfo: nil, + NotificationClientInfo: nil, + SchedulerClientInfo: nil, + CronSchedulerClientInfo: nil, + Registry: nil, + ExpectedResult: true, }, { - Name: "Only Core Data ClientsBootstrap", - CoreDataClientInfo: &coreDataClientInfo, - CommandClientInfo: nil, - MetadataClientInfo: nil, - NotificationClientInfo: nil, - SchedulerClientInfo: nil, - Registry: nil, - ExpectedResult: true, + Name: "Only Core Data ClientsBootstrap", + CoreDataClientInfo: &coreDataClientInfo, + CommandClientInfo: nil, + MetadataClientInfo: nil, + NotificationClientInfo: nil, + SchedulerClientInfo: nil, + CronSchedulerClientInfo: nil, + Registry: nil, + ExpectedResult: true, }, { - Name: "Only Metadata ClientsBootstrap", - CoreDataClientInfo: nil, - CommandClientInfo: nil, - MetadataClientInfo: &metadataClientInfo, - NotificationClientInfo: nil, - SchedulerClientInfo: nil, - Registry: nil, - ExpectedResult: true, + Name: "Only Metadata ClientsBootstrap", + CoreDataClientInfo: nil, + CommandClientInfo: nil, + MetadataClientInfo: &metadataClientInfo, + NotificationClientInfo: nil, + SchedulerClientInfo: nil, + CronSchedulerClientInfo: nil, + Registry: nil, + ExpectedResult: true, }, { - Name: "Only Messaging based Command ClientsBootstrap", - CoreDataClientInfo: nil, - CommandClientInfo: &commandMessagingClientInfo, - MetadataClientInfo: nil, - NotificationClientInfo: nil, - SchedulerClientInfo: nil, - Registry: nil, - ExpectedResult: true, + Name: "Only Messaging based Command ClientsBootstrap", + CoreDataClientInfo: nil, + CommandClientInfo: &commandMessagingClientInfo, + MetadataClientInfo: nil, + NotificationClientInfo: nil, + SchedulerClientInfo: nil, + CronSchedulerClientInfo: nil, + Registry: nil, + ExpectedResult: true, }, } @@ -195,6 +211,10 @@ func TestClientsBootstrapHandler(t *testing.T) { clients[common.SupportSchedulerServiceKey] = test.SchedulerClientInfo } + if test.CronSchedulerClientInfo != nil { + clients[common.SupportCronSchedulerServiceKey] = test.CronSchedulerClientInfo + } + bootstrapConfig := config.BootstrapConfiguration{ Service: &config.ServiceInfo{ RequestTimeout: "30s", @@ -249,6 +269,8 @@ func TestClientsBootstrapHandler(t *testing.T) { subscriptionClient := container.SubscriptionClientFrom(dic.Get) intervalClient := container.IntervalClientFrom(dic.Get) intervalActionClient := container.IntervalActionClientFrom(dic.Get) + scheduleJobClient := container.ScheduleJobClientFrom(dic.Get) + scheduleActionRecordClient := container.ScheduleActionRecordClientFrom(dic.Get) if test.CoreDataClientInfo != nil { assert.NotNil(t, eventClient) @@ -292,6 +314,14 @@ func TestClientsBootstrapHandler(t *testing.T) { assert.Nil(t, intervalActionClient) } + if test.CronSchedulerClientInfo != nil { + assert.NotNil(t, scheduleJobClient) + assert.NotNil(t, scheduleActionRecordClient) + } else { + assert.Nil(t, scheduleJobClient) + assert.Nil(t, scheduleActionRecordClient) + } + if test.Registry != nil { registryMock.AssertExpectations(t) }